[英]Sorting a defaultdict by value in python
I have a data-structure which is something like this:我有一个数据结构是这样的:
The population of three cities for different year are as follows.三个城市不同年份的人口如下。
Name 1990 2000 2010
A 10 20 30
B 20 30 10
C 30 10 20
I am using a defaultdict
to store the data.我正在使用defaultdict
来存储数据。
from collections import defaultdict
cityPopulation=defaultdict(list)
cityPopulation['A']=[10,20,30]
cityPopulation['B']=[20,30,10]
cityPopulation['C']=[30,10,20]
I want to sort the defaultdict
based on a particular column of the list (the year).我想根据列表的特定列(年份)对defaultdict
进行排序。 Say, sorting for 1990, should give C,B,A
, while sorting for 2010 should give A,C,B
.比如说,1990 年的排序应该给出C,B,A
,而 2010 年的排序应该给出A,C,B
。
Also, is this the best way to store the data?另外,这是存储数据的最佳方式吗? As I am changing the population values, I want it to be mutable.当我改变人口值时,我希望它是可变的。
>>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[0],reverse=True) #1990
[('C', [30, 10, 20]), ('B', [20, 30, 10]), ('A', [10, 20, 30])]
>>> sorted(cityPopulation.iteritems(),key=lambda (k,v): v[2],reverse=True) #2010
[('A', [10, 20, 30]), ('C', [30, 10, 20]), ('B', [20, 30, 10])]
Note in python 3 you can't automagically unpack lambda arguments so you would have to change the code注意在 python 3 中你不能自动解包 lambda 参数,所以你必须更改代码
sorted(cityPopulation.items(), key=lambda k_v: k_v[1][2], reverse=True) #2010
If you want to sort based on the values, not in the keys, use data.items()
and set the key with lambda kv: kv[1]
so that it picks the value.如果您想根据值而不是键进行排序,请使用data.items()
并使用lambda kv: kv[1]
设置键,以便它选择值。
See an example with this defaultdict
:请参阅此defaultdict
的示例:
>>> from collections import defaultdict
>>> data = defaultdict(int)
>>> data['ciao'] = 17
>>> data['bye'] = 14
>>> data['hello'] = 23
>>> data
defaultdict(<type 'int'>, {'ciao': 17, 'bye': 14, 'hello': 23})
Now, let's sort by value:现在,让我们按值排序:
>>> sorted(data.items(), lambda kv: kv[1])
[('bye', 14), ('ciao', 17), ('hello', 23)]
Finally use reverse=True
if you want the bigger numbers to come first:如果您希望较大的数字先出现,最后使用reverse=True
:
>>> sorted(data.items(), lambda kv: kv[1], reverse=True)
[('hello', 23), ('ciao', 17), ('bye', 14)]
Note that key=lambda(k,v): v
is a clearer (to me) way to say key=lambda(v): v[1]
, only that the later is the only way Python 3 allows it, since auto tuple unpacking in lambda is not available .请注意, key=lambda(k,v): v
是一种更清晰的(对我来说)说key=lambda(v): v[1]
,只是后者是 Python 3 允许它的唯一方式,因为auto tuple无法在 lambda 中解包。
In Python 2 you could say:在 Python 2 中,你可以说:
>>> sorted(d.items(), key=lambda(k,v): v)
[('bye', 14), ('ciao', 17), ('hello', 23)]
A defaultdict
doesn't hold order. defaultdict
没有顺序。 You might need to use a OrderedDict
, or sort the keys each time as a list.您可能需要使用OrderedDict
,或者每次将键作为列表进行排序。
Eg:例如:
from operator import itemgetter
sorted_city_pop = OrderedDict(sorted(cityPopulation.items()))
Edit: If you just want to print the order, simply use the sorted
builtin:编辑:如果您只想打印订单,只需使用sorted
内置:
for key, value in sorted(cityPopulation.items()):
print(key, value)
Late answer, and not a direct answer to the question, but if you end-up here from a " Sorting a defaultdict by value in python " google search, this is how I sort ( normal python dictionaries cannot be sorted, but they can be printed sorted) a defaultdict
by its values:迟到的答案,而不是对问题的直接回答,但如果你最终从“在 python 中按值排序 defaultdict ”谷歌搜索,这就是我的排序方式(普通的 python 词典无法排序,但它们可以是打印排序)一个defaultdict
按其值:
orders = {
'cappuccino': 54,
'latte': 56,
'espresso': 72,
'americano': 48,
'cortado': 41
}
sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True)
for i in sort_orders:
print(i[0], i[1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.