为什么我没有按预期顺序获得排序函数的结果?
print activities
activities = sorted(activities,key = lambda item:item[1])
print activities
在这种情况下,活动是一个元组列表,如 (start_number,finish_number)
根据我的说法,上述代码的输出应该是根据 finish_number
的递增顺序排序的值列表代码>.当我在 shell 中尝试上面的代码时,我得到了以下输出。我不确定为什么第二个列表没有按照 finish_number
的递增顺序排序。请帮助我理解这一点。
[('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9'), ('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16')]
[('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16'), ('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9')]
print activities
activities = sorted(activities,key = lambda item:item[1])
print activities
Activities in this case is a list of tuples like (start_number,finish_number)
the output of the above code according to me should be the list of values sorted according the the increasing order of finish_number
. When I tried the above code in shell I got the following output. I am not sure why the second list is not sorted according the the increasing order of the finish_number
. Please help me in understanding this.
[('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9'), ('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16')]
[('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16'), ('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9')]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在对字符串而不是整数进行排序:在这种情况下,
10
比4
“小”。要对整数进行排序,请将其转换为:结果:
You are sorting strings instead of integers: in that case,
10
is "smaller" than4
. To sort on integers, convert it to this:Results in:
您的项目将作为字符串而不是数字进行比较。因此,由于按字典顺序
1
字符位于4
之前,因此10
位于4
之前是有意义的。您需要先将值转换为 int:
Your items are being compared as strings, not as numbers. Thus, since the
1
character comes before4
lexicographically, it makes sense that10
comes before4
.You need to cast the value to an int first:
您正在对字符串进行排序,而不是数字。字符串按字符排序。
因此,例如
'40'
大于'100'
,因为字符4
大于1
。您可以通过简单地将项目转换为整数来即时修复此问题。
You are sorting strings, not numbers. Strings get sorted character by character.
So, for example
'40'
is greater than'100'
because character4
is larger than1
.You can fix this on the fly by simply casting the item as an integer.
这是因为您没有将数字存储为数字,而是存储为字符串。字符串
'10'
位于字符串'2'
之前。尝试:It's because you're not storing the number as a number, but as a string. The string
'10'
comes before the string'2'
. Try:寻找更广泛的解决方案来解决您的问题:输入后立即将数据从
str
转换为int
,并将其用作int< /code> (否则你会不断遇到这样的小问题),并将数据格式化为
str
进行输出。此原则普遍适用,例如,在处理非 ASCII 字符串数据时,执行 UTF-8 -> UTF-8 -> UTF-8 -> UTF-8 -> UTF-8 -> UTF-8统一码-> UTF-8;不要尝试操纵未解码的文本。
Look for a BROADER solution to your problem: Convert your data from
str
toint
immediately on input, work with it asint
(otherwise you'll be continually be bumping into little problems like this), and format your data asstr
for output.This principle applies generally, e.g. when working with non-ASCII string data, do UTF-8 -> unicode -> UTF-8; don't try to manipulate undecoded text.