【发布时间】:2021-02-22 12:00:32
【问题描述】:
我正在编写一个函数来将一个值列表迭代到另一个包含字典的列表中,但最后我得到了一个重复值的列表。
def createlayer():
layerdxf = {"2": "",
"70": "64",
"62": "0",
"6": "CONTINUOUS"}
lr = ['grey', 'green', 'red', 'orange']
lrys = []
for la in lr:
layerdxf.update({'2': la})
el = {"LAYER": layerdxf}
lrys.append(el)
return lrys
函数返回:
[
{'LAYER': {'2': 'orange', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'orange', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'orange', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'orange', '70': '64', '62': '0', '6': 'CONTINUOUS'}}
]
什么时候返回:
[
{'LAYER': {'2': 'grey', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'green', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'red', '70': '64', '62': '0', '6': 'CONTINUOUS'}},
{'LAYER': {'2': 'orange', '70': '64', '62': '0', '6': 'CONTINUOUS'}}
]
谢谢,
JA
【问题讨论】:
标签: python-3.x list for-loop