Python .sort() 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1513727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 22:26:46  来源:igfitidea点击:

Python .sort() not working as expected

pythonlistsorting

提问by Dominic Bou-Samra

Tackling a few puzzle problems on a quiet Saturday night (wooohoo... not) and am struggling with sort(). The results aren't quite what I expect. The program iterates through every combination from 100 - 999 and checks if the product is a palindome. If it is, append to the list. I need the list sorted :D Here's my program:

在一个安静的星期六晚上解决一些拼图问题(呜呜……不是),并且正在为 sort() 苦苦挣扎。结果并不完全符合我的预期。该程序遍历从 100 到 999 的每个组合,并检查产品是否为回文。如果是,则附加到列表中。我需要对列表进行排序:D 这是我的程序:

list = [] #list of numbers

for x in xrange(100,1000): #loops for first value of combination
  for y in xrange(x,1000): #and 2nd value
    mult = x*y
    reversed = str(mult)[::-1] #reverses the number
    if (reversed == str(mult)):
      list.append(reversed)

list.sort()
print list[:10]

which nets:

哪个网:

['101101', '10201', '102201', '102201', '105501', '105501', '106601', '108801',
'108801', '110011']

Clearly index 0 is larger then 1. Any idea what's going on? I have a feeling it's got something to do with trailing/leading zeroes, but I had a quick look and I can't see the problem.

很明显,索引 0 比 1 大。知道发生了什么吗?我有一种感觉,这与尾随/前导零有关,但我快速浏览了一下,但看不到问题所在。

Bonus points if you know where the puzzle comes from :P

如果您知道拼图的来源,则加分 :P

回答by Luká? Lalinsky

You are sorting strings, not numbers. '101101' < '10201'because '1' < '2'. Change list.append(reversed)to list.append(int(reversed))and it will work (or use a different sorting function).

您正在排序字符串,而不是数字。'101101' < '10201'因为'1' < '2'。更改list.append(reversed)list.append(int(reversed)),它将起作用(或使用不同的排序功能)。

回答by u0b34a0f6ae

Sort is doing its job. If you intended to store integers in the list, take Luká? advice. You can also tell sort how to sort, for example by making ints:

Sort 正在做它的工作。如果您打算在列表中存储整数,请选择 Luká?建议。您还可以告诉 sort 如何排序,例如通过制作整数:

list.sort(key=int)

the key parameter takes a function that calculates an item to take the list object's place in all comparisons. An integer will compare numerically as you expect.

key 参数采用一个函数,该函数计算一个项目以在所有比较中占据列表对象的位置。整数将按您的预期进行数字比较。

(By the way, listis a really bad variable name, as you override the builtin list() type!)

(顺便说一句,这list是一个非常糟糕的变量名,因为您覆盖了内置的 list() 类型!)

回答by Justin Ethier

Your list contains strings so it is sorting them alphabetically - try converting the list to integers and then do the sort.

您的列表包含字符串,因此它按字母顺序对它们进行排序 - 尝试将列表转换为整数,然后进行排序。

回答by Neil

No need to convert to int. mult already is an int and as you have checked it is a palindrome it will look the same as reversed, so just:

无需转换为int。mult 已经是一个 int 并且您已经检查过它是一个回文,它看起来与反转相同,所以只需:

list.append(mult)

回答by pzr

You're sorting strings, not numbers. Strings compare left-to-right.

您正在排序字符串,而不是数字。字符串从左到右比较。

回答by Dana

You have your numbers stored as strings, so python is sorting them accordingly. So: '101x' comes before '102x' (the same way that 'abcd' will come before 'az').

您将数字存储为字符串,因此 python 会相应地对它们进行排序。所以:'101x' 出现在 '102x' 之前(与 'abcd' 出现在 'az' 之前的方式相同)。

回答by aviraldg

No, it is sorting properly, just that it is sorting lexographicallyand you want numericsorting... so remove the "str()"

不,它正在正确排序,只是它按字典顺序排序并且您想要数字排序......所以删除“str()”

回答by whatnick

The comparator operator is treating your input as strings instead of integers. In string comparsion 2 as the 3rd letter is lexically greater than 1. reversed = str(mult)[::-1]

比较器运算符将您的输入视为字符串而不是整数。在字符串比较 2 中,因为第三个字母在词法上大于 1。 reversed = str(mult)[::-1]