简体   繁体   English

按第一个元素浮点数对包含字符串的元组列表进行排序(Python)

[英]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.

相关问题 使用选择排序功能在Python中对包含元组的列表进行排序 - Sorting a list in Python containing tuples with Selection Sort 如何按元组的第一个元素对元组列表进行排序? - How to sort a list of tuples by their first element? 按浮点数的第一个元素对元组列表进行排序:TypeError:'float'对象不可下标 - Sorting list of tuples by first element that is a float: TypeError: 'float' object is not subscriptable 按Python 3中的特定元组元素对元组列表进行排序 - Sort list of tuples by specific tuple element in Python 3 如果在Python中元组可以为空,如何按最后一个元素对元组列表进行排序? - How to sort list of tuples by the last element if tuples can be empty in Python? 如何按 Python 中的第一个元素重新分类元组列表? - How to recategorize a list of tuples by the first element in Python? 自然地通过元组中Python的第一个元素对字母数字元组进行排序 - Naturally sort a list of alpha-numeric tuples by the tuple's first element in Python Python 使用元组进行列表理解过滤(包含整数作为字符串) - Python list comprehension filtering with tuples (containing integers as strings) 从 Python 中的元组列表列表中获取元组的所有第一个元素 - Get all first element of tuples from list of lists of tuples in Python 按 Python 中字符串中的数字对列表开头包含数字的字符串列表进行排序 - Sort a list of strings containing numbers at the start of the list by the numbers in the strings in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM