[英]Sort List of Tuples Containing Strings By First Element Float (Python)
I have a long list of tuples: 我有很长的元组列表:
[...
(0.862, 'beehive', 'bug'),
(0.12, 'yard', 'property'),
(0.0, 'lake', 'mailbox'),
(0.37, 'maintenance', 'shears'),
(0.1666, 'summer', 'popsicle'),
(0.9, 'poplar', 'tree')
...]
and I need to sort this list descending by the float values. 并且我需要将此列表按float值降序排序。 I know the Python automatically sorts lists by the first value, however even when I call sorted or even explicitly specify the first element, I haven't had success. 我知道Python会自动根据第一个值对列表进行排序,但是即使我调用sorted或显式指定第一个元素,我也没有成功。
sorted(mylist) # doesn't sort the list at all
sorted(mylist, key = x[0]) # on this sort attempt I get "'str' object is not callable"
Can anyone provide detail as to why the list is still disorganized despite these sorting attempts and what might sort by floats in descending order? 任何人都无法提供详细信息,说明尽管进行了这些排序尝试,但为什么列表仍然杂乱无章,并且可能由浮点数按降序排序?
sorted(..)
returns a new list. sorted(..)
返回一个新列表。 What you are looking for is .sort(..)
to sort the list inplace . 你所寻找的是.sort(..)
对列表进行排序就地 。
Furthermore you can use the reverse
parameter to sort in descending order: 此外,您可以使用reverse
参数以降序排序:
data.sort(reverse=True) # sort the list inplace
This will return: 这将返回:
>>> data.sort(reverse=True)
>>> data
[(0.9, 'poplar', 'tree'), (0.862, 'beehive', 'bug'), (0.37, 'maintenance', 'shears'), (0.1666, 'summer', 'popsicle'), (0.12, 'yard', 'property'), (0.0, 'lake', 'mailbox')]
The default sorting of tuples will first sort on the first elements . 默认的元组排序将首先对第一个元素进行排序 。 If these are equal, it will consider the second element of each tuple and so on. 如果这些相等,则将考虑每个元组的第二个元素,依此类推。
If you do not want this tie breaker, but use the original order in that case, you can use an itemgetter
as key
: 如果您不希望使用这种平局决胜局,但在这种情况下使用原始订单,则可以使用itemgetter
作为key
:
from operator import itemgetter
data.sort(reverse=True,key=itemgetter(0)) # sort the list inplace
You can use the same arguments with sorted(..)
if you want to construct a new list that is sorted: 如果要构造一个新的已排序列表,可以将相同的参数与sorted(..)
一起使用:
data_sorted = sorted(data,reverse=True) # construct a new sorted list
Try this way : 尝试这种方式:
data = [
(0.862, 'beehive', 'bug'),
(0.12, 'yard', 'property'),
(0.0, 'lake', 'mailbox'),
(0.37, 'maintenance', 'shears'),
(0.1666, 'summer', 'popsicle'),
(0.9, 'poplar', 'tree')
]
print(*reversed(sorted(data)))
Output : 输出:
(0.9, 'poplar', 'tree') (0.862, 'beehive', 'bug') (0.37, 'maintenance', 'shears') (0.1666, 'summer', 'popsicle') (0.12, 'yard', 'property') (0.0, 'lake', 'mailbox')
Or, You can follow another process : 或者,您可以执行另一个过程:
>>> data = [
... (0.862, 'beehive', 'bug'),
... (0.12, 'yard', 'property'),
... (0.0, 'lake', 'mailbox'),
... (0.37, 'maintenance', 'shears'),
... (0.1666, 'summer', 'popsicle'),
... (0.9, 'poplar', 'tree')
... ]
>>> data.sort(key=lambda tup: tup[0], reverse = True)
>>> data
[(0.9, 'poplar', 'tree'), (0.862, 'beehive', 'bug'), (0.37, 'maintenance', 'shears'), (0.1666, 'summer', 'popsicle'), (0.12, 'yard', 'property'), (0.0, 'lake', 'mailbox')]
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.