[英]Iterate through lists in values of all keys in a dictionary in parallel?
Hello I have a python dictionary that looks like this:您好,我有一本 python 字典,如下所示:
d = {'a': [1,2,3],
'b': [3,4,5],
'c':[6,7,8]}
Is there any way to iterate through this dictionary so that I am getting each item in all list value in all keys in order in parallel?有什么方法可以遍历这本字典,以便我按顺序并行获取所有键中所有列表值中的每个项目? Keep in mind I don't have
pandas
package available.请记住,我没有可用的
pandas
package。
So for example:例如:
loop 1 output: a:1,b:3,c:6
loop 2 output: a:2,b:4,c:7
loop 3 output: a:3,b:5,c:8
You can assume the list lengths are going to be the same for all keys in the dictionary.您可以假设字典中所有键的列表长度都相同。
As a one-liner作为单线
result = [dict(zip(d.keys(), v)) for v in zip(*d.values())]
Or if you want to split things up to make it a bit clearer或者如果你想把事情分开来让它更清楚一点
keys, values = d.keys(), list(zip(*d.values()))
result = [dict(zip(keys, v)) for v in values]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.