Python: sort dictionary with tuples as key by the value

This question already has an answer here: How do I sort a dictionary by value? 38 answers This gives what you want, sorted. But it's not in a dictionary at the end, since you can't sort a dictionary. d={('w1','u1'):3,('w1','u2'):8,('w2','u1'):1,('w1','u3'):11,('w2','u3'):6} d2 = {} for (w,u) , value in d.items(): if w not in d2: d2[w] = [(u,value)] else: d

Python:通过值将字典与元组进行排序

这个问题在这里已经有了答案: 如何按价值对字典进行排序? 38个答案 这给你想要的,排序。 但最终不在字典中,因为你无法对字典进行排序。 d={('w1','u1'):3,('w1','u2'):8,('w2','u1'):1,('w1','u3'):11,('w2','u3'):6} d2 = {} for (w,u) , value in d.items(): if w not in d2: d2[w] = [(u,value)] else: d2[w].append((u, value)) for key, values in d2.items(): print key, ":t",

sort list by frequency

This question already has an answer here: How do I sort a dictionary by value? 38 answers You've to use word_freq.items() here: lis = sorted(word_freq.items(), key = lambda x:x[1], reverse = True) for word,freq in lis: print ("%-10s %d" % (word, freq)) Don't use list as a variable name. 使用collections.Counter来帮助计算事物,并with语句来帮助打开(和关闭)文件。 import collecti

按频率排序

这个问题在这里已经有了答案: 如何按价值对字典进行排序? 38个答案 你必须在这里使用word_freq.items() : lis = sorted(word_freq.items(), key = lambda x:x[1], reverse = True) for word,freq in lis: print ("%-10s %d" % (word, freq)) 不要使用list作为变量名称。 使用collections.Counter来帮助计算事物,并with语句来帮助打开(和关闭)文件。 import collections with open('C:\Temp\Test2.txt', 'r') a

Sort dict by value and return dict, not list of tuples

Possible Duplicate: Python: Sort a dictionary by value d = {"a":4, "b":12, "c":2} If I use sorted() with lambda: s = sorted(d.items(), key=lambda(k,v):(v,k)) I get a list tuples (key,value) but I want a dict again: {"c":2, "a":4, "b":12} And doing dict(the_list_of_tuples) you're back to square one. Standard dict objects are not sorted and so do not guarantee or preserve any ordering

按值排序字典并返回字典,而不是元组列表

可能重复: Python:按值排序字典 d = {"a":4, "b":12, "c":2} 如果我用lambda使用sorted(): s = sorted(d.items(), key=lambda(k,v):(v,k)) 我得到一个列表元组(键,值),但我再次需要一个字典: {"c":2, "a":4, "b":12} 做dict(the_list_of_tuples)你回到dict(the_list_of_tuples) 。 标准dict对象没有排序,所以不保证或保留任何顺序。 这是因为你通常使用一个字典来获取一个键值的排序是不重要的。 如果你

Sort Python dict by datetime value

This question already has an answer here: How do I sort a dictionary by value? 38 answers You could sort the keys like this: sorted(dct, key=dct.get) See the Sorting Mini-HOW TO for an explanation of this and other techniques for sorting. Bearing in mind that the question asks how to sort by the datetime values, here's a possible answer: sorted(dct.items(), key=lambda p: p[1], rever

按日期时间值对Python字典进行排序

这个问题在这里已经有了答案: 如何按价值对字典进行排序? 38个答案 你可以像这样对键进行排序: sorted(dct, key=dct.get) 请参阅Sorting Mini-HOW TO以了解对此和其他排序技术的解释。 记住问题是如何按日期时间值排序,这里有一个可能的答案: sorted(dct.items(), key=lambda p: p[1], reverse=True) => [('raspberry', datetime.datetime(2013, 1, 9, 0, 0)), ('apple', datetime.datetime(2012, 12, 20,

Sort dict by highest value?

This question already has an answer here: How do I sort a dictionary by value? 38 answers How to sort a Python dict by value 5 answers sorted(myDict, key=myDict.get, reverse=True) Here's one way of doing it: >>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8} >>> sorted(myDict.iterkeys(), key=lambda k: myDict[k], reverse=True) ['eight', '

按照最高值排序字典?

这个问题在这里已经有了答案: 如何按价值对字典进行排序? 38个答案 如何按值排序Python字典5个答案 sorted(myDict, key=myDict.get, reverse=True) 这是一种做法: >>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8} >>> sorted(myDict.iterkeys(), key=lambda k: myDict[k], reverse=True) ['eight', 'seven', 'five', 'four', 'two', 'one'] (受这个答案的启

Python: How to sort a dictionary by key

This question already has an answer here: Sorting dictionary keys in python [duplicate] 4 answers How do I sort a dictionary by value? 38 answers How can I sort a dictionary by key? 22 answers Standard Python dictionaries are inherently unordered. However, you could use collections.OrderedDict . It preserves the insertion order, so all you have to do is add the key/value pairs in the

Python:如何按键排序字典

这个问题在这里已经有了答案: 在Python中排序字典键[复制] 4个答案 如何按价值对字典进行排序? 38个答案 我怎样才能按键排序字典? 22个答案 标准的Python词典本质上是无序的。 但是,您可以使用collections.OrderedDict 。 它保留了插入顺序,所以您只需按照所需顺序添加键/值对即可: In [4]: collections.OrderedDict(sorted(result.items())) Out[4]: OrderedDict([('1', 'value1'), ('2', 'value2')]) sorted

sort values and return list of keys from dict python

Possible Duplicate: Python: Sort a dictionary by value I have a dictionary like this: A = {'Name1':34, 'Name2': 12, 'Name6': 46,....} I want a list of keys sorted by the values, ie [Name2, Name1, Name6....] Thanks!!! 使用以get方法作为关键字的sorted (可以通过迭代访问字典关键字): sorted(A, key=A.get) 使用sorted的key参数sorted(d, key=d.get) sorted(a.keys(), key=a.get) 这对键进行排序,

排序值并返回来自dict python的键的列表

可能重复: Python:按值排序字典 我有这样一本字典: A = {'Name1':34, 'Name2': 12, 'Name6': 46,....} 我想要一个按值排序的键列表,例如[Name2, Name1, Name6....] 谢谢!!! 使用以get方法作为关键字的sorted (可以通过迭代访问字典关键字): sorted(A, key=A.get) 使用sorted的key参数sorted(d, key=d.get) sorted(a.keys(), key=a.get) 这对键进行排序,并且对于每个键,都使用a.get来查找要用作排序值的值

Sorting dictionary keys in python

This question already has an answer here: How do I sort a dictionary by value? 38 answers >>> mydict = {'a':1,'b':3,'c':2} >>> sorted(mydict, key=lambda key: mydict[key]) ['a', 'c', 'b'] 我喜欢这一个: sorted(d, key=d.get) my_list = sorted(dict.items(), key=lambda x: x[1])

在Python中对字典键进行排序

这个问题在这里已经有了答案: 如何按价值对字典进行排序? 38个答案 >>> mydict = {'a':1,'b':3,'c':2} >>> sorted(mydict, key=lambda key: mydict[key]) ['a', 'c', 'b'] 我喜欢这一个: sorted(d, key=d.get) my_list = sorted(dict.items(), key=lambda x: x[1])

Add new keys to a dictionary?

Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an .add() method. >>> d = {'key':'value'} >>> print(d) {'key': 'value'} >>> d['mynewkey'] = 'mynewvalue' >>> print(d) {'mynewkey': 'mynewvalue', 'key': 'value'} >>> x = {1:2} >>> print x {1: 2} >>> x.update({3:4}) >>>

将新密钥添加到字典中?

是否可以在创建Python字典后添加一个键? 它似乎没有.add()方法。 >>> d = {'key':'value'} >>> print(d) {'key': 'value'} >>> d['mynewkey'] = 'mynewvalue' >>> print(d) {'mynewkey': 'mynewvalue', 'key': 'value'} >>> x = {1:2} >>> print x {1: 2} >>> x.update({3:4}) >>> print x {1: 2, 3: 4} 我觉得整理有关Python字典的信息: 创建

How to sort a Python dict by value

I have a dict that looks like this { "keyword1":3 , "keyword2":1 , "keyword3":5 , "keyword4":2 } And I would like to convert it DESC and create a list of just the keywords. Eg, this would return ["keyword3" , "keyword1" , "keyword4" , "keyword2"] All examples I found use lambda and I'm not very strong wit

如何按值排序Python字典

我有一个看起来像这样的字典 { "keyword1":3 , "keyword2":1 , "keyword3":5 , "keyword4":2 } 我想将其转换为DESC并创建一个关键字列表。 例如,这将返回 ["keyword3" , "keyword1" , "keyword4" , "keyword2"] 我发现的所有例子都使用lambda,而且我不是那么坚强。 有没有办法通过这个循环,并按照我的方式对它们进行排序 ? 感