{
"group": "avengers",
"members": [
{
"name": "Dr.Strange",
"id": 456409,
"type": "magic",
"relatives": ["wang", "the ancient one"]
},
{
"name": "Iron man",
"id": 381001,
"type": "technology",
"relatives": ["pepper pots", "james rhodes"]
}
]
}
复仇者集团成员:
我想把所有的relatives
分开。我知道如何获取name
、id
和types
,因为它们只有1个值
import json
f = open('1.json', 'r')
content = json.load(f)
for i in content['members']:
print(i['name'], end=' ')
print(i['id'], end=' ')
print(i['relatives'])
print()
Dr.Strange 456409 ['wang', 'the ancient one']
Iron man 381001 ['pepper pots', 'james rhodes']
但我不想让单独的亲戚出现在名单上。因此,对于每个亲戚,请重新键入我在上表中列出的名称
我知道我可以使用i['relatives'][0]
,但是如果我不知道亲戚的数量呢?也许for loop
能帮上忙
您可以随时检查列表的长度,并在该长度上进行循环迭代
我想这可能会有帮助
例如,您可以这样做:
是的,
for
循环可以工作。尝试:输出:
相关问题 更多 >
编程相关推荐