两层循环添加列表元素之后,发现数据一致(id地址问题!)。

res = [{‘id‘: 1, ‘type‘: 1, ‘value‘: 1.9}, {‘id‘: 2, ‘type‘: 2, ‘value‘: 2.9}, {‘id‘: 2, ‘type‘: 4, ‘value‘: 3.9},
{‘id‘: 1, ‘type‘: 2, ‘value‘: 2.9}, {‘id‘: 2, ‘type‘: 3, ‘value‘: 3.9}]
rem = [{‘r_id‘: 1, ‘name‘: ‘小白‘, ‘sex‘: ‘男‘}, {‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘},
{‘r_id‘: 4, ‘name‘: ‘黑铁疙瘩‘, ‘sex‘: ‘女‘}]

# 需求:通过r_id与id,获取不同的type对应的value值

# ret = []
#
# for i in res:
# for j in rem:
# if i[‘id‘] == j[‘r_id‘]:
# j[‘type‘] = i[‘type‘]
# j[‘value‘] = i[‘value‘]
# ret.append(j)
# else:
# if j not in ret:
# ret.append(j)
# print(ret)


获取到的结果是:
"""
[{‘r_id‘: 1, ‘name‘: ‘小白‘, ‘sex‘: ‘男‘, ‘type‘: 2, ‘value‘: 2.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9},
{‘r_id‘: 4, ‘name‘: ‘黑铁疙瘩‘, ‘sex‘: ‘女‘},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9},
{‘r_id‘: 1, ‘name‘: ‘小白‘, ‘sex‘: ‘男‘, ‘type‘: 2, ‘value‘: 2.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9}]
"""


查看到发现是id地址重复问题


ret = []
import copy

for i in res:
for j in rem:
if i[‘id‘] == j[‘r_id‘]:
j[‘type‘] = i[‘type‘]
j[‘value‘] = i[‘value‘]
b = j.copy().copy()
ret.append(b)
# print(j)
# print(i)
else:
if j not in ret:
ret.append(j)
print(ret)
"""
[{‘r_id‘: 1, ‘name‘: ‘小白‘, ‘sex‘: ‘男‘, ‘type‘: 1, ‘value‘: 1.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9},
{‘r_id‘: 4, ‘name‘: ‘黑铁疙瘩‘, ‘sex‘: ‘女‘},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 2, ‘value‘: 2.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 4, ‘value‘: 3.9},
{‘r_id‘: 1, ‘name‘: ‘小白‘, ‘sex‘: ‘男‘, ‘type‘: 2, ‘value‘: 2.9},
{‘r_id‘: 2, ‘name‘: ‘小黑‘, ‘sex‘: ‘女‘, ‘type‘: 3, ‘value‘: 3.9}]
"""




两层循环添加列表元素之后,发现数据一致(id地址问题!)。

上一篇:CCS的使用


下一篇:windows查看端口占用以及关闭相应的进程