通过Dicts中的不同值Value查找所有Key-Elements

时间:2017-06-19 13:44:54

标签: python python-2.7 list sorting dictionary

我有两个数组我想用两个数组制作一个字典,但我面临的问题是我希望重复的值应该像列表一样重复。我正在使用这段代码

text_results = ['hello', 'foo' , 'hi' , 'good' , 'this' , 'hi' ]
scores = [4,2,4,5,1,4]
dictionary = dict(zip(scores,text_result)) 

我希望输出看起来像这样

[(4, 'hello'), (2, 'foo'), (4, 'hi') ,(5, 'good') ,(1, 'this'),(4, 'hi')]

我怎样才能按照这样的降序对其进行排序:

[(5, 'good') ,(4, 'hello'),(4, 'hi'), (4, 'hi'), (2, 'foo') ,(1, 'this')]

4 个答案:

答案 0 :(得分:2)

sorted tuples

If you want a list of sorted tuples, you can just sort them with sorted and a reverse order :

print(sorted(zip(scores, text_results), reverse=True))
# [(5, 'good'), (4, 'hi'), (4, 'hi'), (4, 'hello'), (2, 'foo'), (1, 'this')]

values as lists

If you really want a dict, your values could be lists. Python dicts are unordered, though :

text_results = ['hello', 'foo' , 'hi' , 'good' , 'this' , 'hi' ]
scores = [4,2,4,5,1,4]

table = {}

for name, score in zip(text_results, scores):
    if not table.get(name):
        table[name] = []
    table[name].append(score)

print(table)
# {'this': [1], 'hi': [4, 4], 'foo': [2], 'hello': [4], 'good': [5]}

This way, if you want the values for hi, you can get them directly without having to iterate over a list:

>>> table.get('hello')
[4]
>>> table.get('hi')
[4, 4]
>>> table.get('not_here')
>>> 

Note that this functionality is provided by collections.defaultdict with defaultdict(list). I sometimes find the extra output distracting, though:

defaultdict(<type 'list'>, {'this': [1], 'hi': [4, 4], 'foo': [2], 'hello': [4], 'good': [5]})

答案 1 :(得分:1)

然后你应该使用list而不是字典。

text_results = ['hello', 'foo' , 'hi' , 'good' , 'this' , 'hi' ]
scores = [4,2,4,5,1,4]
res_list = list(zip(scores,text_results)) 
print res_list

答案 2 :(得分:1)

A dictionary can map a key to only one value. So you cannot construct a this as a vanilla dictionary. There are some options:

  1. you work with a list (in with zip(..), in with list(zip(..))):

    >>> zip(scores,text_results)
    [(4, 'hello'), (2, 'foo'), (4, 'hi'), (5, 'good'), (1, 'this'), (4, 'hi')]
    
  2. you work with a MultiDict, you can for instance install werkzeug:

    $ pip install werkzeug
    

    and then you can use the multidict:

    from werkzeug.datastructures import MultiDict
    d = MultiDict(zip(scores,text_results))

    you can then use .getlist(key) to obtain a list of values associated with a key, for example:

    >>> d.getlist(4)
    ['hello', 'hi', 'hi']
    >>> d.getlist(2)
    ['foo']
    
  3. you can use a defaultdict and build the dictionary that maps to a list yourself:

    from collections import defaultdict
    
    d = defaultdict(list)
    for k,v in zip(scores,text_results):
        d[k].append(v)
    

答案 3 :(得分:0)

If you want something like this output : [(4, 'hello'), (2, 'foo'), (4, 'hi') ,(5, 'good') ,(1, 'this'),(4, 'hi')]
Then it is not a dictionary in python meaning. The dictionary in python use {}.
What you call "dictionary" is a list, each element being a tuple. So you should just create a list, and add a tuple for each iteration of your text or score list :

if __name__ == "__main__":
    text_results = ['hello', 'foo', 'hi', 'good', 'this', 'hi']
    scores = [4, 2, 4, 5, 1, 4]
    my_own_container = []
    for i in range(len(text_results)):
        my_own_container.append(tuple((scores[i], text_results[i])))
    print(my_own_container)