有人可以解释为什么这有效吗? (Python 迭代器)
Could someone please explain why this works? (Python Iterator)
我有一本字典,里面有一些宠物的名字和它们相应的信息,还有一个主人名字的列表。该代码的目标是通过从列表(所有者)中提取名称并在字典中创建新键:值对来更新字典。
owners = ["adam", "sandra", "ashley"]
pets = {
"buster": {
"type": "dog",
"colour": "black and white",
"disposition": "sassy",
},
"jojo": {
"type": "cat",
"colour": "grey",
"disposition": "grumpy",
},
"amber": {"type": "cat", "colour": "black", "disposition": "playful"},
}
iterator = iter(pets.values())
for owner in owners:
for pet_info in iterator:
try:
pet_info["owner"] = owner
break
except StopIteration:
print("The end")
print(pets)
Output
{
'buster': {'type': 'dog', 'colour': 'black and white', 'disposition': 'sassy', 'owner': 'adam'},
'jojo': {'type': 'cat', 'colour': 'grey', 'disposition': 'grumpy', 'owner': 'sandra'},
'amber': {'type': 'cat', 'colour': 'black', 'disposition': 'playful', 'owner': 'ashley'}
}
使用 iter() 函数后,我能够生成我想要的输出。因此,我试图弄清楚 iter() 函数是如何使这成为可能的。
(我做了 google 但搜索结果不是我要找的)
提前致谢!
你可以这样做,前提是 'pets' 中的键数与 'owners' 中的元素数相同:
owners = ["adam", "sandra", "ashley"]
pets = {
"buster": {
"type": "dog",
"colour": "black and white",
"disposition": "sassy",
},
"jojo": {
"type": "cat",
"colour": "grey",
"disposition": "grumpy",
},
"amber": {
"type": "cat",
"colour": "black",
"disposition": "playful"}}
for p, o in zip(pets.values(), owners):
p['owner'] = o
print(pets)
pet.values() 是一个列表,而 iter(pet.values()) 给你一个 list_iterator。
检查以下示例:
a=[1,2,3]
ia = iter(a)
for i in a:
print(i)
break
for i in a:
print(i)
break
for i in a:
print(i)
break
for i in ia:
print(i)
break
for i in ia:
print(i)
break
for i in ia:
print(i)
break
输出只是说明了你在使用 iter() 函数时可能忽略的情况。
我有一本字典,里面有一些宠物的名字和它们相应的信息,还有一个主人名字的列表。该代码的目标是通过从列表(所有者)中提取名称并在字典中创建新键:值对来更新字典。
owners = ["adam", "sandra", "ashley"]
pets = {
"buster": {
"type": "dog",
"colour": "black and white",
"disposition": "sassy",
},
"jojo": {
"type": "cat",
"colour": "grey",
"disposition": "grumpy",
},
"amber": {"type": "cat", "colour": "black", "disposition": "playful"},
}
iterator = iter(pets.values())
for owner in owners:
for pet_info in iterator:
try:
pet_info["owner"] = owner
break
except StopIteration:
print("The end")
print(pets)
Output
{
'buster': {'type': 'dog', 'colour': 'black and white', 'disposition': 'sassy', 'owner': 'adam'},
'jojo': {'type': 'cat', 'colour': 'grey', 'disposition': 'grumpy', 'owner': 'sandra'},
'amber': {'type': 'cat', 'colour': 'black', 'disposition': 'playful', 'owner': 'ashley'}
}
使用 iter() 函数后,我能够生成我想要的输出。因此,我试图弄清楚 iter() 函数是如何使这成为可能的。 (我做了 google 但搜索结果不是我要找的)
提前致谢!
你可以这样做,前提是 'pets' 中的键数与 'owners' 中的元素数相同:
owners = ["adam", "sandra", "ashley"]
pets = {
"buster": {
"type": "dog",
"colour": "black and white",
"disposition": "sassy",
},
"jojo": {
"type": "cat",
"colour": "grey",
"disposition": "grumpy",
},
"amber": {
"type": "cat",
"colour": "black",
"disposition": "playful"}}
for p, o in zip(pets.values(), owners):
p['owner'] = o
print(pets)
pet.values() 是一个列表,而 iter(pet.values()) 给你一个 list_iterator。 检查以下示例:
a=[1,2,3]
ia = iter(a)
for i in a:
print(i)
break
for i in a:
print(i)
break
for i in a:
print(i)
break
for i in ia:
print(i)
break
for i in ia:
print(i)
break
for i in ia:
print(i)
break
输出只是说明了你在使用 iter() 函数时可能忽略的情况。