[英]How to iterate through a list and a dictionary in a nested for loop
I am using python 3.9 and attempting to take information from a python list and a python dictionary and iterate through it for a loop.我正在使用 python 3.9 并尝试从 python 列表和 python 字典中获取信息并遍历它以进行循环。 The correct email address will be taken from the dictionary depending on the unit.
将根据单元从字典中获取正确的 email 地址。
When I execute my code, it loops three times over each member of the list, and I don't know how to make it not do that.当我执行我的代码时,它会在列表的每个成员上循环三遍,我不知道如何让它不这样做。 I believe the initial for loop is executed ok and gets the metal, but is it the second loop that's making it run three times per item, and how do I fix it?
我相信最初的 for 循环执行得很好并且得到了金属,但是第二个循环使它每个项目运行 3 次,我该如何解决它?
I realize this is pretty noddy, but I must be making fundamental mistakes somewhere, and after trying to figure it out for the last 5 hours, it's now time to ask for some help.我意识到这很愚蠢,但我一定是在某个地方犯了根本性的错误,在过去 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)
and here is the output:这是 output:
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.
Just remove the inner for
loop, changing this:只需删除内部
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')
to this:对此:
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')
There's no rule that says you need to be iterating over metals
in order to call metals.get
.没有规则说您需要遍历
metals
才能调用metals.get
。
There are 3 items in metals.items()
. metals.items()
中有 3 个项目。 This is why the loop runs 3x.这就是循环运行 3 次的原因。 Just remove that statement;
只需删除该声明; you don't need that loop
你不需要那个循环
for unit in units:
if unit == gold:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.