2017-06-19 57 views
2

我有一个两个数组我想使用两个数组来创建一个字典,但是我面临的问题是我想重复的值应该重复多少在列表中。我使用这段代码通过不同的值查找所有键元素值在字典中

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')] 
+0

但是,这是一个元组列表和你所要求做一本字典?请确定你真的想要做什么。 – idjaw

+0

“*但是我面对的问题是我想重复的值应该重复多少它在列表中,我使用这段代码*”,那句话对我来说不清楚...... –

+0

你描述了什么是一个列表,你可以使用'list(zip(scores,text_results))',或者在Python-2.x中,更简单的'zip(scores,text_results)'...... –

回答

1

那么你应该使用列表,而不是字典。

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 
+0

但是如果我想按照分数升序来安排呢? –

+0

@AliJafar'排序(res_list)'。降序:'排序(res_list,reverse = True)'。 –

+0

谢谢@ZevAverbach –

1

字典可以映射键只有一个值。所以你不能构造一个这样的香草字典。有一些选项:

  1. 你一个列表的工作(在zip(..),在list(zip(..))):

    >>> zip(scores,text_results) 
    [(4, 'hello'), (2, 'foo'), (4, 'hi'), (5, 'good'), (1, 'this'), (4, 'hi')] 
    
  2. 你有MultiDict工作

    ,例如你可以安装werkzeug

    $ pip install werkzeug 
    

    然后你可以使用multidic T:

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

    那么你可以使用.getlist(key)获得与键关联的值的列表,例如:

    >>> d.getlist(4) 
    ['hello', 'hi', 'hi'] 
    >>> d.getlist(2) 
    ['foo'] 
    
  3. 可以使用defaultdict并建立映射到一个列表中的字典自己:

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

这很酷,没有听说过MultiDict。与'defaultdict(list)'相比,有什么优势? –

+1

@ZevAverbach:我认为它提供了一个很好的界面,使您仍然可以使用映射到值的键。例如,查找'd [key]'会给出与该键相关的第一个值。 –

2

排序的元组

如果要排序的元组的列表,你可以用sorted和反向顺序进行排序:

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

值列表

如果你真的想要一个dict,你的价值观可能是名单。 Python的类型的字典是无序的,但:

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]} 

这样,如果你想为hi的值,你可以直接得到它们,而不必遍历一个列表:

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

注意,此功能是由collections.defaultdictdefaultdict(list)提供。我有时会发现额外的输出分心,虽然:

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

如果你想是这样的输出:[(4, 'hello'), (2, 'foo'), (4, 'hi') ,(5, 'good') ,(1, 'this'),(4, 'hi')]
那么它是不是在python意义的字典。 Python中的字典使用{}
你称之为“dictionary”的是一个列表,每个元素都是一个元组。 所以你应该创建一个列表,并添加一个元组为您的文本或成绩榜的每次迭代:

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) 
+0

如果我想根据得分按升序排列,该怎么办? –

+0

@AliJafar也许你应该在你的问题中添加一些你的工作环境。但我认为排序足以解决您的问题。 –

相关问题