简体   繁体   English

Pythonic将两个不同长度的列表压缩成字典

[英]Pythonic zipping two lists, differing length, into dictionary

I have two lists of strings with different lengths: 我有两个长度不同的字符串列表:

k = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
v = ['z', 'y', 'x', 'w',]
dict = {}

I want to have the following output: 我想要以下输出:

{'a': ['z', 'y', 'x', 'w',], 'b': ['z', 'y', 'x', 'w',], 'c': ['z', 'y', 'x', 'w',], 'd': ['z', 'y', 'x', 'w',], 'e': ['z', 'y', 'x', 'w',], 'f': ['z', 'y', 'x', 'w',], 'g': ['z', 'y', 'x', 'w',]}    

The closest I have gotten is this: 我得到的最接近的是:

{key: [key, value] for key, value in zip(k, v)}

and

(kvalue,vvalue) for kkey, kvalue in enumerate(k) for vkey, vvalue in enumerate(v)

The ultimate goal with this is to use the key, value pairs to insert the contained strings into a string in a separate for loop. 最终目的是使用键值对将包含的字符串插入单独的for循环中的字符串中。 I'm using Python 3.6. 我正在使用Python 3.6。

Pseudocode: 伪代码:

for filenames in directory:
    var = 'some string' + each key/value pairing
    print(var)

>>> some string a z
>>> some string a y
>>> some string a x
>>> some string a w
>>> some string b z
>>> some string b y
>>> some string b x
>>> some string b w
>>> some string c z
>>> some string c y
>>> some string c x
>>> some string c w
>>> some string d z
>>> some string d y
>>> some string d x
>>> some string d w
>>> some string e z
>>> some string e y
>>> some string e x
>>> some string e w
>>> some string f z
>>> some string f y
>>> some string f x
>>> some string f w
>>> some string g z
>>> some string g y
>>> some string g x
>>> some string g w

For the first part of your question, use dict.fromkeys() : 对于问题的第一部分,请使用dict.fromkeys()

k = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
v = ['z', 'y', 'x', 'w',]
d = dict.fromkeys(k, v)

output: 输出:

{'a': ['z', 'y', 'x', 'w'],
 'b': ['z', 'y', 'x', 'w'],
 'c': ['z', 'y', 'x', 'w'],
 'd': ['z', 'y', 'x', 'w'],
 'e': ['z', 'y', 'x', 'w'],
 'f': ['z', 'y', 'x', 'w'],
 'g': ['z', 'y', 'x', 'w']}

For the second part, you actually don't need the first, use itertools.product : 对于第二部分,您实际上不需要第一部分,请使用itertools.product

from itertools import product

k = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
v = ['z', 'y', 'x', 'w',]

out = ['some string {} {}'.format(key, val) for key, val in product(k, v)]

output: 输出:

['some string a z',
 'some string a y',
 'some string a x',
 'some string a w',
 'some string b z',
 'some string b y',
 'some string b x',
 'some string b w',
 'some string c z',
 'some string c y',
 'some string c x',
 'some string c w',
 'some string d z',
 'some string d y',
 'some string d x',
 'some string d w',
...

You can use a dictionary-comprehension: 您可以使用字典理解:

result = {x: v for x in k}
print(result)
{'a': ['z', 'y', 'x', 'w'], 'b': ['z', 'y', 'x', 'w'], 'c': ['z', 'y', 'x', 'w'], 'd': ['z', 'y', 'x', 'w'], 'e': ['z', 'y', 'x', 'w'], 'f': ['z', 'y', 'x', 'w'], 'g': ['z', 'y', 'x', 'w']}

This creates a dictionary with keys x from k and values v . 这产生与键的字典xk和值v

A dictionary comprehension would do: 字典理解可以做到:

k = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
v = ['z', 'y', 'x', 'w',]
print({key: v for key in k})

# {'a': ['z', 'y', 'x', 'w',], 'b': ['z', 'y', 'x', 'w',], 'c': ['z', 'y', 'x', 'w',], 'd': ['z', 'y', 'x', 'w',], 'e': ['z', 'y', 'x', 'w',], 'f': ['z', 'y', 'x', 'w',], 'g': ['z', 'y', 'x', 'w',]} 

You're using the wrong method altogether: zip is for corresponding pairs only, giving you (az) (by) (cx) and so on, without providing the other pairings. 您完全使用了错误的方法: zip仅适用于对应的配对,给您(az) (by) (cx)等,而没有提供其他配对。

Is there some reason you need to generate this more than once? 是否有某些原因需要多次生成此信息? If not, this is a job for itertools.product , not zip . 如果不是,这是itertools.product的工作,而不是zip

from itertools import product

k = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
v = ['z', 'y', 'x', 'w',]

for pair in product(k, v):
    print("some string " + ' '.join(pair))

Output: 输出:

some string a z
some string a y
some string a x
some string a w
some string b z
some string b y
some string b x
some string b w
...
some string g x
some string g w

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

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