list1=[{"status":1, "s_count":100}, {"status":2, "s_count":20}]
list2=[{"status":1, "s_count":0}, {"status":3, "s_count":30}]
list1.extend(list2)
给出了
[{"status":1, "s_count":100}, {"status":2, "s_count":20}, {"status":1,
"s_count":0}, {"status":3, "s_count":30}]
我想要的是,
[{"status":1, "s_count":0}, {"status":2, "s_count":20}, {"status":3, "s_count":30}]
答案 0 :(得分:1)
1)考虑使用字典而不是列表。
2)如果你坚持使用清单:
helper = []
for i in range(0,len(list1)):
for item in list2:
if item['status'] == list1[i]['status']:
list1[i]['s_count'] = item['s_count']
helper.add(item)
for item in helper:
list2.remove(item)
list1.extend(list2)
效率低下,如果可能的话尽量做1。