[英]python to update dict values in list
我有一個包含兩個元素(鍵值對)的列表。 兩個元素都有相同的鍵。 但是很少有不同的值,如下所示。
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']}
結果alist應該是這樣的,
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'}]
感謝您的幫助。
新答案 :
根據a_dict
的值更新alist
。
>>> 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值
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'}]
另一種替代方式是:
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)
你需要遍歷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'}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.