[英]Python: dictionary with keys having multiple values. How to print dictionary as set of dictionaries separating the list of values
I have a dictionary having keys with multiple values like this: 我有一本字典,其键具有多个值,如下所示:
my_dict = {'key0': (0, 1), 'key1': (a, b), 'key2': (x, y)}
I would like print my dictionary like 我想像打印我的字典
my_dict = {'key0': 0, 'key1': a, 'key2': x}, {'key0': 1, 'key1': b, 'key2': y}
How can I achieve this? 我该如何实现? Please help...
请帮忙...
If all tuples are of the same length (ie 2) you could use a list comprehension: 如果所有元组的长度相同(即2),则可以使用列表推导:
[{key: value[i] for key, value in my_dict} for i in range(2)]
Here is an example: 这是一个例子:
my_dict = {'key0': (0, 1), 'key1': (1, 2), 'key2': ('a', 'b')}
[{key: value[i] for key, value in my_dict.iteritems()} for i in range(2)]
Output: 输出:
[{'key0': 0, 'key1': 1, 'key2': 'a'}, {'key0': 1, 'key1': 2, 'key2': 'b'}]
there is no data structure like a = {}, {}
. 没有像a =
{}, {}
这样的数据结构。 But, you can split the initial dict into 2 dicts: 但是,您可以将初始字典分为2个字典:
my_dict = {'key0': (0, 1), 'key1': ('a', 'b'), 'key2': ('x', 'y')}
my_dict_1 = dict((key, value[0]) for key, value in my_dict.iteritems())
my_dict_2 = dict((key, value[1]) for key, value in my_dict.iteritems())
Results: 结果:
print my_dict_1
{'key0': 0, 'key1': 'a', 'key2': 'x'}
print my_dict_1
{'key0': 1, 'key1': 'b', 'key2': 'y'}
Assuming all of your values are tuples/lists but could be of different sizes, you could do the following: 假设所有值都是元组/列表,但大小可能不同,则可以执行以下操作:
def expand_dict(input_dict):
max_size = max(len(v) for v in input_dict.values())
output_list = [dict() for _ in range(max_size)]
for k, v in input_dict.items():
for i, x in enumerate(v):
output_list[i][k] = x
return output_list
my_dict = {'key0': (0, 1), 'key1': ('a', 'b'), 'key2': ('x', 'y', 'z')}
print expand_dict(my_dict)
This prints: 打印:
[{'key2': 'x', 'key1': 'a', 'key0': 0}, {'key2': 'y', 'key1': 'b', 'key0': 1}, {'key2': 'z'}]
You could try like this: 您可以这样尝试:
>>> my_dict = {'key0': (0, 1), 'key1': (2, 3), 'key2': (4, 5)}
>>> dict_list = []
>>> for key in my_dict:
... for index, item in enumerate(my_dict[key]):
... if len(dict_list) >= index + 1:
... dict_list[index][key] = item
... else:
... dict_list.append({key: item})
...
>>> dict_list
[{'key2': 4, 'key1': 2, 'key0': 0}, {'key2': 5, 'key1': 3, 'key0': 1}]
This is quite efficient from the point of view of processing time and also covers all possible dict length cases (what if one of the tuples has 3 values not 2). 从处理时间的角度来看,这是非常有效的,并且还涵盖了所有可能的dict长度情况(如果一个元组具有3个值而不是2个值,该怎么办)。
Extended solution with complex example: 具有复杂示例的扩展解决方案:
Used functions: zip()
and itertools.zip_longest()
使用的函数:
zip()
和itertools.zip_longest()
import itertools
my_dict = {'key0': (0, 1), 'key1': ('a', 'b'), 'key2': ('x', 'y'), 'key3': ('z', 'v')}
result = [dict(t) for t in itertools.zip_longest(*[list(zip([k,k],v)) for k,v in my_dict.items()])]
print(result)
The output: 输出:
[{'key2': 'x', 'key0': 0, 'key1': 'a', 'key3': 'z'}, {'key2': 'y', 'key0': 1, 'key1': 'b', 'key3': 'v'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.