简体   繁体   中英

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. The correct email address will be taken from the dictionary depending on the unit.

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?

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.

# 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:

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 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 .

There are 3 items in metals.items() . This is why the loop runs 3x. Just remove that statement; you don't need that loop

for unit in units:
    if unit == gold:
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM