[英]Sorting list of tuples by first element of each tuple - Python 2.7
I have a list of tuples: 我有一个元组列表:
lst = [('Q4-2005', 327.93), ('Q1-2005', 133.05), ('Q3-2005', 500.95), ('Q2-2005', 254.22)]
I want to sort this list by the first element of each tuple. 我想按每个元组的第一个元素对该列表进行排序。 Thus my resulting, sorted list should look like:
因此,我得到的排序列表应如下所示:
[('Q1-2005', 133.05), ('Q2-2005', 254.22), ('Q3-2005', 500.95), ('Q4-2005', 327.93)]
I tried doing this using sorting() and lambda, but the resulting list is not sorted. 我尝试使用sorting()和lambda进行此操作,但结果列表未排序。 Im thinking its because my 'dates' are actually strings:
我之所以这样想是因为我的“日期”实际上是字符串:
sorted(lst, key = lambda x: x[0])
So i guess I have 2 questions.. 所以我想我有2个问题。
First, How do I sort the list of tuples so that they are in chronological order? 首先,如何对元组列表进行排序,使它们按时间顺序排列?
Second, what is the best way to make python realize that 'Q1-2005' is a date? 其次,使python意识到“ Q1-2005”是日期的最佳方法是什么? Eventually I want to plot this data where x-axis is the 'date' and y-axis is the number associated with each date?
最终我想绘制此数据,其中x轴是“日期”,y轴是与每个日期关联的数字?
You said that you want Python to interpret Q1-2005
as a date. 您说过要Python将
Q1-2005
解释为日期。 If you convert the list to such a format, then sorting becomes trivial, and plotting will also be easier. 如果将列表转换为这种格式,则排序变得很简单,并且绘制也将更加容易。 Here's one way to do it using
datetime.date
(it encodes the quarters as a date representing the first date of the quarter). 这是使用
datetime.date
编码的一种方法(它将季度编码为代表季度第一个日期的日期)。
from datetime import date
# date() takes in year, month, day args
date_lst = [(date(int(q[3:]), 3 * int(q[1]) - 2, 1), v)
for q, v in lst]
This will result in something like this: 这将导致如下所示:
[(datetime.date(2005, 10, 1), 327.93),
(datetime.date(2005, 1, 1), 133.05),
(datetime.date(2005, 7, 1), 500.95),
(datetime.date(2005, 4, 1), 254.22)]
After that, sorting chronologically is as simple as sorted(date_lst)
. 之后,按时间顺序
sorted(date_lst)
就像sorted(date_lst)
一样简单。
The best way to do this, redefining a sort, would be to make a new class or a new function. 重新定义排序的最佳方法是创建新类或新函数。 The default sort algorithms in Python use traditional lexicographical sorting.
Python中的默认排序算法使用传统的字典排序。 Calling sort(tuple) will sort them in alphabetical/alphanumeric order, but not necessarily the order you want.
调用sort(tuple)将按字母/字母数字顺序对其进行排序,但不一定按您想要的顺序进行。
I will note, sorting by default will sort the tuples based on their index [0] value, only going to index [1] if two first indices are the same. 我将注意到,默认情况下,排序将根据元组的索引[0]值对其进行排序,如果两个第一个索引相同,则仅对索引[1]进行排序。
不要命名您的变量列表!!!
sorted(the_list, key=lambda x: list(reversed(map(int,x[0][1:].split("-")))))
Using an object with __lt__
and/or __cmp__
would be good, but if you want to use a key function: 将对象与
__lt__
和/或__cmp__
一起使用会很好,但是如果要使用键功能:
#!/usr/local/cpython-3.3/bin/python
list_ = [('Q4-2005', 327.93), ('Q1-2005', 133.05), ('Q3-2005', 500.95), ('Q2-2005', 254.22), ('Q1-2006', 123.45), ('Q2-2007', 678.90), ]
def key(element):
subresult1 = element[0]
subresult2 = subresult1.split('-')
subresult2.reverse()
subresult2[0] = int(subresult2[0])
return subresult2
list_.sort(key=key)
print(list_)
Which out for the years - they're more significant than the quarters. 多年来,它们比宿舍更重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.