Python sorted() function not working the way it should(Python sorted() 函数无法正常工作)
问题描述
基本上我有一个嵌套列表,我试图对第一个索引进行排序我复制了 python howto 说的方法,但它似乎不起作用,我不明白为什么:
来自网站的代码:
<预><代码>>>>student_tuples = [('约翰', 'A', 15),('简', 'B', 12),('戴夫', 'B', 10),]>>>sorted(student_tuples, key=lambda student: student[2]) # 按年龄排序[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]我的代码:
def print_scores(self):尝试:#打开txt并读取数据,然后将数据分成以-"分隔的列表f = open(appdata + "scores.txt", "r")fo = f.read()f.close()用户列表 = fo.split('
')sheet_list = []对于用户列表中的用户:sheet = user.split('-')如果 len(sheet) != 2:继续sheet_list.append(sheet)sorted(sheet_list, key = lambda ele : ele[1]) #这里是复制的部分!如果 len(sheet_list) >20: # 只打印前 20 个分数sheet_list = sheet_list[len(sheet_list) - 21 :len(sheet_list) - 1]#在漂亮的表格中打印分数打印姓名分数"对于 sheet_list 中的用户:尝试:名称 = 用户 [0]分数 = 用户 [1]大小 = len(名称)对于范围内的 x(0,14):如果 x >尺寸 - 1:sys.stdout.write(" ")别的:sys.stdout.write(name[x])sys.stdout.write(score + "
")除了:打印 ""除了:打印没有要显示的分数!"
问题是打印出来的列表和txt里的一模一样,好像排序功能什么都没做!
示例:
txt 文件中的数据:
Jerry-1284汤姆-264巴里-205omgwtfbbqhaxomgsss-209长颈鹿-1227
打印的内容:
名称分数杰瑞 1284汤姆 264巴里 205omgstfbbqhaxom209长颈鹿 1227
sorted
返回一个新列表.如果要修改现有列表,请使用
sheet_list.sort(key = lambda ele : ele[1])
Basically I have a nested list that I am trying to sort through the 1'st index I copied the way that the python howto says how to do it but it doesn't seem to work and I don't understand why:
code from the website:
>>> student_tuples = [
('john', 'A', 15),
('jane', 'B', 12),
('dave', 'B', 10),
]
>>> sorted(student_tuples, key=lambda student: student[2]) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
My code:
def print_scores(self):
try:
#opening txt and reading data then breaking data into list separated by "-"
f = open(appdata + "scores.txt", "r")
fo = f.read()
f.close()
userlist = fo.split('
')
sheet_list = []
for user in userlist:
sheet = user.split('-')
if len(sheet) != 2:
continue
sheet_list.append(sheet)
sorted(sheet_list, key = lambda ele : ele[1]) #HERE IS THE COPIED PART!
if len(sheet_list) > 20: # only top 20 scores are printed
sheet_list = sheet_list[len(sheet_list) - 21 :len(sheet_list) - 1]
#prints scores in a nice table
print "name score"
for user in sheet_list:
try:
name = user[0]
score = user[1]
size = len(name)
for x in range(0,14):
if x > size - 1:
sys.stdout.write(" ")
else:
sys.stdout.write(name[x])
sys.stdout.write(score + "
")
except:
print ""
except:
print "no scores to be displayed!"
The bug is that the resulting printed list is exactly like how it was in the txt as if the sorting function didn't do anything!
Example:
Data in txt file:
Jerry-1284
Tom-264
Barry-205
omgwtfbbqhaxomgsss-209
Giraffe-1227
What's printed:
Name Score
Jerry 1284
Tom 264
Barry 205
omgstfbbqhaxom209
Giraffe 1227
sorted
returns a new list. If you want to modify the existing list, use
sheet_list.sort(key = lambda ele : ele[1])
这篇关于Python sorted() 函数无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python sorted() 函数无法正常工作


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01