如何在包含字典的列表中循环
How to loop inside a list that contains a dictionary
我首先尝试在实体列表中循环,然后我想在 id=1 的聊天源中循环。在那个id=2之后,然后是它的聊天源1,2,3。等等。
这个结构不能解决我的问题。
entities = [
{id: 1, "chat_sources": [1, 2, 3]},
{id: 2, "chat_sources": [1, 2, 3]},
{id: 3, "chat_sources": [1, 2, 3]}
]
for i in entities:
for j in entities["chat_sources"]:
print(j)
你快完成了:
entities = [
{id: 1, "chat_sources": [1, 2, 3]},
{id: 2, "chat_sources": [1, 2, 3]},
{id: 3, "chat_sources": [1, 2, 3]}
]
for entity in entities: # entity is already the item rather than its index
for j in entity["chat_sources"]: # note it's entity, not entities
print(j)
for elem in entities:
print(*elem['chat_sources'], sep='\n')
我首先尝试在实体列表中循环,然后我想在 id=1 的聊天源中循环。在那个id=2之后,然后是它的聊天源1,2,3。等等。
这个结构不能解决我的问题。
entities = [
{id: 1, "chat_sources": [1, 2, 3]},
{id: 2, "chat_sources": [1, 2, 3]},
{id: 3, "chat_sources": [1, 2, 3]}
]
for i in entities:
for j in entities["chat_sources"]:
print(j)
你快完成了:
entities = [
{id: 1, "chat_sources": [1, 2, 3]},
{id: 2, "chat_sources": [1, 2, 3]},
{id: 3, "chat_sources": [1, 2, 3]}
]
for entity in entities: # entity is already the item rather than its index
for j in entity["chat_sources"]: # note it's entity, not entities
print(j)
for elem in entities:
print(*elem['chat_sources'], sep='\n')