简体   繁体   English

Python:具有多个值的键的字典。 如何将字典打印为分隔值列表的字典集

[英]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.

相关问题 Python:具有一个键和多个值的字典:如何获取具有相同SET值的键列表? - Python : Dictionary with one key and multiple values : How to get list of keys having same SET of values? 如何在字典列表中打印字典值? - how to print dictionary values in a list of dictionaries? 键和列表作为其值的字典。 如何识别列表中的项目,然后移至另一个列表? (蟒蛇) - A dictionary of keys and lists as their values. How to identify item in the list, and move to another list? (python) Python - 如何从另一个列表创建一个字典列表,该列表具有两个具有多个值的键? - Python - How to create a dictionary list from another list which having two keys with multiple values? 如何在Python的列表中打印字典的键和值 - How to print keys and values of a dictionary which is contained in a list in Python 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values? 如何在python中打印字典键和值 - How to print dictionary keys and values in python 比较两个字典键并使用Python中的列表值创建字典 - Compare two dictionaries keys and create dictionary with list values in Python 如何将列表中的字典组合成一个字典,并添加相同键的值? - How to combine dictionaries in a list into one dictionary with values of same keys added? 用逗号分隔值打印字典 - Print Dictionary with commas separating values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM