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