简体   繁体   English

如何获取不同字典中相同键的所有值,并且字典存储在列表中

[英]how can i get all the values for the same key in different dictionary and dictionaries are stored in a list

from collections import defaultdict

some_dict = defaultdict(list)

index_dict = dict.fromkeys(range(506))

for d in (index_dict,idx_pred_value[0],idx_pred_value[1],idx_pred_value[2],idx_pred_value[3],idx_pred_value[4],idx_pred_value[5],idx_pred_value[6],idx_pred_value[7],idx_pred_value[8],idx_pred_value[9],idx_pred_value[10],idx_pred_value[11],idx_pred_value[12],idx_pred_value[13],idx_pred_value[14],idx_pred_value[15],idx_pred_value[16],idx_pred_value[17],idx_pred_value[18],idx_pred_value[19],idx_pred_value[20],idx_pred_value[21],idx_pred_value[22],idx_pred_value[23],idx_pred_value[24],idx_pred_value[25],idx_pred_value[26],idx_pred_value[27],idx_pred_value[28],idx_pred_value[29]):
    for key, value in d.items():
        some_dict[key].append(value)

here idx_pred_value is my list of dictionaries.这里 idx_pred_value 是我的字典列表。 each dicitonary contains some value with respect to a index value i want to combine all value with same index(key)每个字典都包含一些关于索引值的值我想将所有值与相同的索引(键)结合起来

although from above code i am able to that but is there any generalize way to access dictionary rather mentioning each dicitionary as individual element of the list虽然从上面的代码我能够做到,但是有没有任何通用的方法来访问字典,而不是将每个字典作为列表的单个元素提及

You could do :你可以这样做:

list_of_dicts = [index_dict]+[idx_pred_value[i] for i in range(30)]
for d in list_of_dicts:

instead of explicitly writing out the list of dictionaries.而不是明确写出字典列表。

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

相关问题 在字典字典中查找相同键的不同值 - Find different values of the same key in a dictionary of dictionaries 如何从字典列表中获取所有值的列表? - How can I get a list of all values from a list of dictionaries? 如何从字典列表中的字典键中获取所有值 - How to get all values from a key of dictionaries which are in a list which is in a dictionary 如何使用 for 循环从存储在列表中的所有字典中的键返回值? - How do I use a for loop to return values from a key that features in all the dictionaries stored in a list? 在列表的不同字典中添加相同键的值 - Adding values of the same key in different dictionaries in a list 如何将字典列表转换为具有相同键和值总和的字典? - How can I convert a list of dictionaries to a dictionary with same keys and sum of the values? 如果字典的键相同,如何添加字典列表的值(列表)? - How do I add values (list) of a list of dictionaries if their key is same? 如何将值列表转换为字典列表,并为每个值添加相同的键? - How can I turn a list of values to a list of dictionaries with the same key added to each value? 访问嵌套字典中不同字典的相同键的所有值 - Accesssing all values from same key for different dictionaries within nested dictionary Python:如何过滤字典列表以获取一个键的所有值 - Python: How to filter a list of dictionaries to get all the values of one key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM