简体   繁体   English

我有两个列表,我想从中创建一个字典。 但是我想为一个键存储多个值

[英]I have two list and I want to create a dictionary from it . But I want to store multiple values for one key

I have two list as below我有两个列表如下

idx = [344, 344, 590]
newx = [(257, 381), (260, 368), (514, 245)]

I used the below code to make dictionary but it neglected one value.我用下面的代码来制作字典,但它忽略了一个值。 I want to store multiple values with one key.我想用一个键存储多个值。

res = dict(zip(idx, newx))
print(res)

output looks like {344: (260, 368), 590: (514, 245)} but I want output to be {344: [(257, 381),(260, 368)], 590: [(514, 245)]} output 看起来像{344: (260, 368), 590: (514, 245)}但我希望 output 是{344: [(257, 381),(260, 368)], 590: [(514, 245)]}

Use a defaultdict:使用默认字典:

from collections import defaultdict

idx = [344, 344, 590]
newx = [(257, 381), (260, 368), (514, 245)]

# default to empty list
res = defaultdict(list)
for k, v in zip(idx, newx):
    res[k].append(v)

# get rid of default value
res = dict(res)

You can not do that as dict objects have unique keys.您不能这样做,因为dict对象具有唯一键。 You should just the use list of tuple:你应该只使用元组列表:

idx = [344, 344, 590]
newx = [(257, 381), (260, 368), (514, 245)]

res = zip(idx, newx)
print(list(res))

If you want to access all the values based on the key, use collections.defaultdict如果要访问基于键的所有值,请使用collections.defaultdict

I believe this will do what you want.我相信这会做你想做的。 Bonus, this won't require an import.奖金,这不需要进口。

I hope this helps.我希望这有帮助。

res = {}
for k, v in zip(idx, newx):
    if k in res:
        res[k].append(v)
    else:
        res[k] = [v]

I have two list as below我有两个列表如下

idx = [344, 344, 590]
newx = [(257, 381), (260, 368), (514, 245)]

I used the below code to make dictionary but it neglected one value.我使用下面的代码制作字典,但它忽略了一个值。 I want to store multiple values with one key.我想用一个键存储多个值。

res = dict(zip(idx, newx))
print(res)

output looks like {344: (260, 368), 590: (514, 245)} but I want output to be {344: [(257, 381),(260, 368)], 590: [(514, 245)]} output 看起来像{344: (260, 368), 590: (514, 245)}但我希望 output 是{344: [(257, 381),(260, 368)], 590: [(514, 245)]} 3148),(2960, (3), (2960, (3), 5 {344: [(257, 381),(260, 368)], 590: [(514, 245)]}

暂无
暂无

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

相关问题 我有两个列表,一个用于x值,一个用于y值,我想从这些列表中创建一个列表 - I have two lists one for x values and one for y values, I want to make a list from these 我想从字典元素创建列表 - I want to create list from dictionary elements 我已经使用列表作为每个键的值制作了一个字典,我想打印没有方括号的值 - I have made a dictionary using a list as the values for each key, I want to print the values without square brackets 我有一本包含值列表的字典。 我想从值列表中取最高值 - I have a dictionary with lists of values. I want to take the highest value from the list of values 如何使用我想要获取的字典的一个键/值对从字典列表中访问字典 - How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch 我想向字典中添加一个包含多个随机值的“文本”列表 - I want to add a 'texts' list with multiple random values to the dictionary 我在列表中有 dict,我想从 value 中获取 key - I have dict in list, I want to get key from value 如何为列表中的一个键创建具有多个值的 Python 字典,然后创建具有一列和多行的 pandas 数据框 - How can I create a Python dictionary with multiple values for one key from a list, to then create a pandas dataframe with one column and multiple rows 我有两本字典,如下所述。 我想将它合并到一个嵌套字典中 - i have two dictionary as stated below. i want to merge it into one nested dictionary 我有一个字典值列表,我想要列表的第一个索引。 如何遍历它? - I have a list in values of a dictionary and I want the first index of the list. How to iterate through it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM