为什么我没有按预期顺序获得排序函数的结果?

发布于 2024-12-10 10:05:39 字数 647 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魔法少女 2024-12-17 10:05:39

您正在对字符串而不是整数进行排序:在这种情况下,104 “小”。要对整数进行排序,请将其转换为:

activites = sorted(activities,key = lambda item:int(item[1]))
print activities

结果:

[('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9'), ('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16')]

You are sorting strings instead of integers: in that case, 10 is "smaller" than 4. To sort on integers, convert it to this:

activites = sorted(activities,key = lambda item:int(item[1]))
print activities

Results in:

[('1', '4'), ('3', '5'), ('0', '6'), ('5', '7'), ('3', '9'), ('5', '9'), ('6', '10'), ('8', '11'), ('8', '12'), ('2', '14'), ('12', '16')]
烂人 2024-12-17 10:05:39

您的项目将作为字符串而不是数字进行比较。因此,由于按字典顺序 1 字符位于 4 之前,因此 10 位于 4 之前是有意义的。

您需要先将值转换为 int:

activities  = sorted(activities,key = lambda item:int(item[1]))

Your items are being compared as strings, not as numbers. Thus, since the 1 character comes before 4 lexicographically, it makes sense that 10 comes before 4.

You need to cast the value to an int first:

activities  = sorted(activities,key = lambda item:int(item[1]))
山有枢 2024-12-17 10:05:39

您正在对字符串进行排序,而不是数字。字符串按字符排序。

因此,例如 '40' 大于 '100',因为字符 4 大于 1

您可以通过简单地将项目转换为整数来即时修复此问题。

 activities  = sorted(activities,key = lambda item: int(item[1]))

You are sorting strings, not numbers. Strings get sorted character by character.

So, for example '40' is greater than '100' because character 4 is larger than 1.

You can fix this on the fly by simply casting the item as an integer.

 activities  = sorted(activities,key = lambda item: int(item[1]))
纵山崖 2024-12-17 10:05:39

这是因为您没有将数字存储为数字,而是存储为字符串。字符串 '10' 位于字符串 '2' 之前。尝试:

activities = sorted(activities, key=lambda i: int(i[1]))

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:

activities = sorted(activities, key=lambda i: int(i[1]))
几味少女 2024-12-17 10:05:39

寻找更广泛的解决方案来解决您的问题:输入后立即将数据从 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 to int immediately on input, work with it as int (otherwise you'll be continually be bumping into little problems like this), and format your data as str 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文