[英]Python Append items to dictionary in a loop
I am trying to append values to a dictionary inside a loop, but somehow it's only appending one of the values.我正在尝试将 append 值添加到循环内的字典中,但不知何故它只是附加了其中一个值。 I recreated the setup using the same numbers I am dynamically getting.
我使用动态获取的相同数字重新创建了设置。
The output from "print(vertex_id_from_shell)" is "{0: [4], 1: [12], 2: [20]}". “print(vertex_id_from_shell)”中的 output 是“{0: [4], 1: [12], 2: [20]}”。 I need to keep the keys, but add the remaining numbers to the values.
我需要保留键,但将剩余的数字添加到值中。
Thanks.谢谢。
shells = {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 1: [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27], 2: [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]}
uvsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 9, 8, 1, 10, 11, 3, 12, 0, 2, 13, 14, 15, 16, 17, 17, 16, 18, 19, 19, 18, 20, 21, 21, 20, 22, 23, 15, 24, 25, 16, 26, 14, 17, 27, 28, 29, 30, 31, 31, 30, 32, 33, 33, 32, 34, 35, 35, 34, 36, 37, 29, 38, 39, 30, 40, 28, 31, 41]
vertsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 1, 0, 1, 7, 5, 3, 6, 0, 2, 4, 8, 9, 11, 10, 10, 11, 13, 12, 12, 13, 15, 14, 14, 15, 9, 8, 9, 15, 13, 11, 14, 8, 10, 12, 16, 17, 19, 18, 18, 19, 21, 20, 20, 21, 23, 22, 22, 23, 17, 16, 17, 23, 21, 19, 22, 16, 18, 20]
vertex_id_from_shell = {}
for shell in shells:
selection_shell = shells.get(shell)
#print(selection_shell)
for idx, item in enumerate(selection_shell):
if item in uvsID:
uv_index = uvsID.index(item)
vertex_ids = vertsID[uv_index]
vertex_id_from_shell[shell] = [ ( vertex_ids ) ]
print(vertex_id_from_shell)
#{0: [4], 1: [12], 2: [20]}
#desired result
{0: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 7, 0, 1, 7, 5, 6, 4], 1: [8, 9, 11, 10, 13, 12, 15, 14, 9, 8, 15, 13, 14, 12], 2: [16, 17, 19, 18, 21, 20, 23, 22, 17, 16, 23, 21, 22, 20]}
You're overwriting vertex_id_from_shell[shell]
each time through the loop, not appending to it.您每次通过循环覆盖
vertex_id_from_shell[shell]
,而不是附加到它。
Use collections.defaultdict()
to automatically create the dictionary elements with an empty list if necessary, then you can append.如有必要,使用
collections.defaultdict()
自动创建具有空列表的字典元素,然后您可以使用 append。
from collections import defaultdict
vertex_id_from_shell = defaultdict(list)
for shell, selection_shell in shells.items():
for item in selection_shell:
if item in uvsID:
uv_index = uvsID.index(item)
vertex_ids = vertsID[uv_index]
vertex_id_from_shell[shell].append(vertex_ids)
You are setting vertex_id_from_shell[shell]
to a new list, containing only one item every time.您正在将
vertex_id_from_shell[shell]
设置为一个新列表,每次仅包含一项。 Instead, you should append to it.相反,你应该 append 给它。
But first, of course that list needs to exist, so you should check and create it if it doesn't already exist.但首先,该列表当然需要存在,所以如果它不存在,您应该检查并创建它。
shells = {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 1: [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27], 2: [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]}
uvsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 9, 8, 1, 10, 11, 3, 12, 0, 2, 13, 14, 15, 16, 17, 17, 16, 18, 19, 19, 18, 20, 21, 21, 20, 22, 23, 15, 24, 25, 16, 26, 14, 17, 27, 28, 29, 30, 31, 31, 30, 32, 33, 33, 32, 34, 35, 35, 34, 36, 37, 29, 38, 39, 30, 40, 28, 31, 41]
vertsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 1, 0, 1, 7, 5, 3, 6, 0, 2, 4, 8, 9, 11, 10, 10, 11, 13, 12, 12, 13, 15, 14, 14, 15, 9, 8, 9, 15, 13, 11, 14, 8, 10, 12, 16, 17, 19, 18, 18, 19, 21, 20, 20, 21, 23, 22, 22, 23, 17, 16, 17, 23, 21, 19, 22, 16, 18, 20]
vertex_id_from_shell = {}
for shell in shells:
selection_shell = shells.get(shell)
#print(selection_shell)
for idx, item in enumerate(selection_shell):
if item in uvsID:
uv_index = uvsID.index(item)
vertex_ids = vertsID[uv_index]
# if the list does not exist, create it
if shell not in vertex_id_from_shell:
vertex_id_from_shell[shell] = []
# append to list
vertex_id_from_shell[shell].append(vertex_ids)
print(vertex_id_from_shell)
# {0: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 7, 5, 6, 4],
# 1: [8, 9, 11, 10, 13, 12, 15, 14, 9, 8, 15, 13, 14, 12],
# 2: [16, 17, 19, 18, 21, 20, 23, 22, 17, 16, 23, 21, 22, 20]}
Can you be more specifically?你能更具体一点吗? What result do you expected to get?
你期望得到什么结果? You can give an example.
你可以举个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.