简体   繁体   English

Python字典打印所有键的所有值

[英]Python dictionary print all values for all keys

I have two python lists: 我有两个python列表:

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

The "keys" are cluster ID list for the corresponding words in "values" list. “键”是“值”列表中相应单词的簇ID列表。 I wish to print key-value pairs using 我希望使用打印键值对

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

dictionary = dict(zip(keys, values))
for key, value in dictionary.items() :
    print (key, value)

But it only prints 但它只打印

1 apple
2 paper
3 tennis

What I actually want is to get all values for all keys like this 我真正想要的是获得所有这些键的所有值

1 [apple]
2 [book,pen,paper]
3 [soccer,tennis]

I know that my current code should logically print the first output as keys are unique. 我知道我的当前代码应该逻辑上打印第一个输出,因为键是唯一的。 But how can I change it so that it will print all values for all keys? 但是如何更改它以便打印所有键的所有值? Thank you in advance! 先感谢您!

from collections import defaultdict

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

d = defaultdict(list)
for k, v in zip(keys, values):
    d[k].append(v)

Looks like what you want is a mapping from one key to multiple values, one way to accomplish it would be: 看起来你想要的是从一个键到多个值的映射,实现它的一种方法是:

from collections import defaultdict

d = defaultdict(list)

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

for tuple in zip(keys, values):
    d[tuple[0]].append(tuple[1])

print(d)  # defaultdict(<class 'list'>, {1: ['apple'], 2: ['book', 'pen', 'paper'], 3: ['soccer', 'tennis']})

You can use itertools : 你可以使用itertools

import itertools
keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']
final_data = {a:[i[0] for i in b] for a, b in [(a, list(b)) for a, b in itertools.groupby(sorted(zip(values, keys), key=lambda x:x[-1]), key=lambda x:x[-1])]}

Output: 输出:

{1: ['apple'], 2: ['book', 'pen', 'paper'], 3: ['soccer', 'tennis']}

pure python also works 纯python也有效

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

d = dict(zip(keys, [[] for _ in keys]))  # dict w keys, empty lists as values

for k, v in zip(keys, values):
    d[k].append(v)

d
Out[128]: {1: ['apple'], 2: ['book', 'pen', 'paper'], 3: ['soccer', 'tennis']}

Two method : 两种方法:

If you want you can use default dict as many already have been suggested : 如果你想要,你可以使用默认的dict,因为已经建议了许多:

Data is : 数据是:

keys=[1,2,2,3,2,3]
values=['apple','book','pen','soccer','paper','tennis']

Method: 1 方法:1

import collections


d=collections.defaultdict(list)

for i in zip(keys,values):
    d[i[0]].append(i[1])

print(d)

output: 输出:

{1: ['apple'], 2: ['book', 'pen', 'paper'], 3: ['soccer', 'tennis']}

Or if you want to develop your own logic without importing any external module then you can try: 或者,如果您想在不导入任何外部模块的情况下开发自己的逻辑,那么您可以尝试:

result={}
for i in zip(keys,values):
    if i[0] not in result:
        result[i[0]]=[i[1]]
    else:
        result[i[0]].append(i[1])

print(result)

output: 输出:

{1: ['apple'], 2: ['book', 'pen', 'paper'], 3: ['soccer', 'tennis']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM