如何在嵌套的 for 循环中遍历列表和字典
How to iterate through a list and a dictionary in a nested for loop
我正在使用 python 3.9 并尝试从 python 列表和 python 字典中获取信息并循环遍历它。将根据单位从字典中提取正确的电子邮件地址。
当我执行我的代码时,它在列表的每个成员上循环三次,我不知道如何让它不那样做。我相信初始的 for 循环执行正常并获得了金属,但这是第二个循环使每个项目 运行 三次,我该如何修复它?
我意识到这很不靠谱,但我一定是在某个地方犯了根本性错误,在过去 5 个小时试图解决这个问题之后,现在是寻求帮助的时候了。
# Dictionary of metals and email addresses
metals = {
'gold':'1@gmail.com',
'silver':'2@gmail.com',
'platinum':'3@gmail.com',
}
# Variable holding some string data
gold = """
Gold is a chemical element with the symbol Au and atomic number 79,
"""
silver = """
Silver is a chemical element with the symbol Ag and atomic number 47.
"""
platinum = """
Platinum is a chemical element with the symbol Pt and atomic number 78.
"""
units = [gold, silver, platinum]
# What I want to do is have a loop where it takes the item in the list
#, for example, gold, matches it with the key to that in the dictionary, thereby
# enabling me to send gold to 1@gmail.com, silver to 2@gmail.com, and platinum to
# 3gmail.com
for unit in units:
for metal in metals.items():
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
# just some code to print out various bits of information
# Print our Dictionary Keys
for k in metals.keys():
print('Keys: ' + k)
# Print our dictionary Values
for v in metals.values():
print('Values: ' + v)
# print out the values held in our list
for item in units:
print('Items: ' + item)
这是输出:
1@gmail.com
1@gmail.com
1@gmail.com
2@gmail.com
2@gmail.com
2@gmail.com
3@gmail.com
3@gmail.com
3@gmail.com
Keys: gold
Keys: silver
Keys: platinum
Values: 1@gmail.com
Values: 2@gmail.com
Values: 3@gmail.com
Items:
Gold is a chemical element with the symbol Au and atomic number 79,
Items:
Silver is a chemical element with the symbol Ag and atomic number 47.
Items:
Platinum is a chemical element with the symbol Pt and atomic number 78.
只需删除内部 for
循环,更改为:
for unit in units:
for metal in metals.items():
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
对此:
for unit in units:
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
没有规则规定您需要迭代 metals
才能调用 metals.get
。
metals.items()
中有 3 个项目。这就是循环运行 3 次的原因。只需删除该声明;你不需要那个循环
for unit in units:
if unit == gold:
...
我正在使用 python 3.9 并尝试从 python 列表和 python 字典中获取信息并循环遍历它。将根据单位从字典中提取正确的电子邮件地址。
当我执行我的代码时,它在列表的每个成员上循环三次,我不知道如何让它不那样做。我相信初始的 for 循环执行正常并获得了金属,但这是第二个循环使每个项目 运行 三次,我该如何修复它?
我意识到这很不靠谱,但我一定是在某个地方犯了根本性错误,在过去 5 个小时试图解决这个问题之后,现在是寻求帮助的时候了。
# Dictionary of metals and email addresses
metals = {
'gold':'1@gmail.com',
'silver':'2@gmail.com',
'platinum':'3@gmail.com',
}
# Variable holding some string data
gold = """
Gold is a chemical element with the symbol Au and atomic number 79,
"""
silver = """
Silver is a chemical element with the symbol Ag and atomic number 47.
"""
platinum = """
Platinum is a chemical element with the symbol Pt and atomic number 78.
"""
units = [gold, silver, platinum]
# What I want to do is have a loop where it takes the item in the list
#, for example, gold, matches it with the key to that in the dictionary, thereby
# enabling me to send gold to 1@gmail.com, silver to 2@gmail.com, and platinum to
# 3gmail.com
for unit in units:
for metal in metals.items():
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
# just some code to print out various bits of information
# Print our Dictionary Keys
for k in metals.keys():
print('Keys: ' + k)
# Print our dictionary Values
for v in metals.values():
print('Values: ' + v)
# print out the values held in our list
for item in units:
print('Items: ' + item)
这是输出:
1@gmail.com
1@gmail.com
1@gmail.com
2@gmail.com
2@gmail.com
2@gmail.com
3@gmail.com
3@gmail.com
3@gmail.com
Keys: gold
Keys: silver
Keys: platinum
Values: 1@gmail.com
Values: 2@gmail.com
Values: 3@gmail.com
Items:
Gold is a chemical element with the symbol Au and atomic number 79,
Items:
Silver is a chemical element with the symbol Ag and atomic number 47.
Items:
Platinum is a chemical element with the symbol Pt and atomic number 78.
只需删除内部 for
循环,更改为:
for unit in units:
for metal in metals.items():
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
对此:
for unit in units:
if unit == gold:
email_address = metals.get('gold')
print(email_address)
elif unit == silver:
email_address = metals.get('silver')
print(email_address)
elif unit == platinum:
email_address = metals.get('platinum')
print(email_address)
else:
print('No Match')
没有规则规定您需要迭代 metals
才能调用 metals.get
。
metals.items()
中有 3 个项目。这就是循环运行 3 次的原因。只需删除该声明;你不需要那个循环
for unit in units:
if unit == gold:
...