简体   繁体   English

在2D清单上排序

[英]Sorting on a 2D list

I have a 2D list that I have created from reading in a txt file. 我有一个2D列表,是通过阅读txt文件创建的。 I have written this code to sort the list by the highest value - position 5. 我已编写此代码以按最高值(位置5)对列表进行排序。

sort = list(sorted(sort,key=lambda x: x[5],reverse=True))
for i in range( 0, len( sort ) ):
            print( "Name:",sort[i][0],"\tscore1:",sort[i][1],"\tscore2:",sort[i][2],"\tscore3:",sort[i][3], "\tHighest:",sort[i][5] )

The code seems to work apart from that for some reason it put's 10 (the only 2 digit number at the bottom of the list rather than at the top. I have use the max function to attain the highest number and then simply appended it to the end of each list. 由于某些原因,该代码似乎与之不同,它把10(唯一的2位数字放在列表的底部而不是顶部)。我使用max函数获得最高的数字,然后将其简单地附加到每个列表的末尾。

Name: B score1: 6 score2: 1 score3: 3 Highest: 6 名称:B得分1:6得分2:1得分3:3最高:6

Name: Z score1: 4 score2: 4 score3: 5 Highest: 5 名称:Z得分1:4得分2:4得分3:5最高:5

Name: Neil score1: 4 score2: 1 score3: 3 Highest: 4 名称:Neil得分1:4得分2:1得分3:3最高:4

Name: fred score1: 10 score2: 0 score3: 0 Highest: 10 名称:fred得分1:10得分2:0得分3:0最高:10

Any help appreciated. 任何帮助表示赞赏。

The problem is almost certainly that you don't actually have numbers, but strings.* 问题几乎可以肯定是您实际上没有数字,而是字符串。*

Strings are compared lexicographically—that is, alphabetical order, character by character, the same way you'd sort words in a dictionary. 字符串按字典顺序进行比较,即按字母顺序,按字符进行比较,这与对字典中的单词进行排序的方式相同。 Digits aren't anything special; 数字没什么特别的; they're treated just like any other character. 他们像对待其他任何角色一样被对待。 So, "10" is smaller than "2" because the first character of the first string, "1" , is smaller than the first string of the second character, "2" . 因此, "10"小于"2"因为第一个字符串"1"的第一个字符小于第二个字符"2"的第一个字符串。

What you probably want to do is store integers (or floats, or some other numeric type) in the first place. 您可能想做的是首先存储整数(或浮点数或其他数字类型)。

But if you want to store strings and sort by their integer value, you can pass int as the sorting key. 但是,如果要存储字符串并按其整数值进行排序,则可以将int作为排序键。 Or, in your case, lambda x: int(x[5]) instead of lambda x: x[5] . 或者,在您的情况下,应使用lambda x: int(x[5])而不是lambda x: x[5]


* For debugging in the future: when you just print a value, it can be ambiguous what it is. *对于将来的调试:仅print一个值时,它可能是模棱两可的。 The number 2 and the string "2" both just print out as 2 . 数字2和字符串"2"都只打印为2 It can help to temporarily print(repr(x)) and/or print(type(x)) to find problems like this. 它可以帮助临时print(repr(x))和/或print(type(x))来查找类似的问题。 (You'd get 2 vs. '2' and int vs. str , respectively.) Even the repr can still occasionally be ambiguous—but this mostly only happens in cases where it usually doesn't matter. (您将分别得到2 vs.'2 '2'int vs. str 。)即使repr 有时仍可能是模棱两可的-但这仅在通常无关紧要的情况下才会发生。 Mostly. 大多。

This is because x[5] seems to be a string. 这是因为x[5]似乎是一个字符串。 You should try converting it to an int by calling int(x[5]) . 您应该尝试通过调用int(x[5])将其转换为int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM