我有一个包含两个元素(键值对)的列表。两个元素都有相同的键。但是很少有不同的值,如下所示。
alist = [{u'a': u'x', u'b': u'y', u'c': u'z'}, {u'a': u'x', u'b': u'm', u'c': u'n'}]
我想更新新数据,以便从字典中提供,这是从用户输入更新的,如下所示。
a_dict = {'a': ['user_input_x'], 'b': ['user_input_y', 'user_input_m'], 'c': ['user_input_z', 'user_input_n']}
结果列表应该如下所示,
ans_alist = [{u'a': u'user_input_x', u'b': u'user_input_y', u'c': u'user_input_z'}, {u'a': u'user_input_x', u'b': u'user_input_m', u'c': u'user_input_n'}]
我正在尝试一些事情,下面是片段,但是因为它是dict,代码正在更新所有键,具有相同的值,
for i in range(0, 2):
for key in alist:
key['a'] = a_dict['a'][i]
key['b'] = a_dict['b'][i]
key['c'] = a_dict['b'][i]
print alist
ans_alist = [{u'a': ['user_input_x'], u'b': 'user_input_y', u'c': 'user_input_n'}, {u'a': ['user_input_x'], u'b': 'user_input_y', u'c': 'user_input_n'}]
感谢您的帮助。
答案 0 :(得分:2)
新答案:
根据alist
中的值更新a_dict
。
>>> new_alist = []
>>> for i,d in enumerate(alist): #traverse the list
temp = {}
for key,val in d.items(): #go through the `dict`
if len(a_dict[key])>0 : #alist->[key] is present in a_dict
temp[key] = a_dict[key].pop(0) #used the value, delete it from a_dict
else : #use previous updated value
temp[key] = new_alist[i-1][key] #get value from previously updated
new_alist.append(temp) #add the updated `dict`
#driver values
IN : alist = [{u'a': u'x', u'b': u'y', u'c': u'z'},
{u'a': u'x', u'b': u'm', u'c': u'n'}]
IN : a_dict = {'a': ['user_input_x'], 'b': ['user_input_y', 'user_input_m'], 'c': ['user_input_z', 'user_input_n']}
OUT : new_alist
=> [{'a': 'user_input_x', 'b': 'user_input_y', 'c': 'user_input_z'},
{'a': 'user_input_x', 'b': 'user_input_m', 'c': 'user_input_n'}]
旧答案 :(完全不符合要求)
计算a_dict
:
>>> from collections import defaultdict
>>> a_dict = defaultdict(list)
>>> for d in alist: #traverse the list
for key,val in d.items():
if val not in a_dict[key]: #if val not already there
a_dict[key].append(val) #add it to the [key]
>>> a_dict
=> defaultdict(<class 'list'>,
{'a': ['user_input_x'],
'b': ['user_input_y', 'user_input_m'],
'c': ['user_input_z', 'user_input_n']
})
计算ans_list
:
>>> ans_list = []
>>> for d in alist: #traverse the list
temp = {}
for key,val in d.items():
temp[key] = val
ans_list.append(temp) #add the new dictionary
>>> ans_list
=> [{'a': 'user_input_x', 'b': 'user_input_y', 'c': 'user_input_z'},
{'a': 'user_input_x', 'b': 'user_input_m', 'c': 'user_input_n'}]
答案 1 :(得分:1)
另一种替代方式是:
from collections import defaultdict
repeat_count = len(alist)
ans_alist = [defaultdict(dict) for _ in range(repeat_count)]
for k, v in a_dict.items():
mul_times = repeat_count / len(v) # Use // if Python 3
extend_counter = repeat_count % (mul_times * len(v))
repeat_v = v * mul_times + v[:extend_counter]
unicode_k = u'{}'.format(k)
for index, val in enumerate(repeat_v):
ans_alist[index][unicode_k] = u'{}'.format(val)
答案 2 :(得分:0)
你需要遍历dicts列表,一次抓取一个dict。 然后迭代其键并弹出结果列表中的项目:
new_alist = []
for d in alist:
new_dict = {}
for k in d.keys():
if a_dict[k]:
new_dict[k] = a_dict[k].pop(0)
new_alist.append(new_dict)
>>> new_alist
[{u'a': 'user_input_x', u'c': 'user_input_z', u'b': 'user_input_y'}, {u'c':
'user_input_n', u'b': 'user_input_m'}]