简体   繁体   中英

Python for loops stops after first iteration

I'm having trouble debugging my get_secondary_connections function below. For some reason, my for friend in network[user]['connections'] loop always stops at the first value in the list instead of looping through the entire list. I don't understand why that's the case. Can someone give me some guidance on this? Thanks so much!

Here's my network:

network = { 

'Freda': {'connections': ['Olive', 'John', 'Debra'], 'favorite games': ['Starfleet Commander', 'Ninja Hamsters', 'Seahorse Adventures']}, 

'Ollie': {'connections': ['Mercedes', 'Freda', 'Bryant'], 'favorite games': ['Call of Arms', 'Dwarves and Swords', 'The Movie: The Game']}, 

'Debra': {'connections': ['Walter', 'Levi', 'Jennie', 'Robin'], 'favorite games': ['Seven Schemers', 'Pirates in Java Island', 'Dwarves and Swords']}, 

'Olive': {'connections': ['John', 'Ollie'], 'favorite games': ['The Legend of Corgi', 'Starfleet Commander']}, 

'Levi': {'connections': ['Ollie', 'John', 'Walter'], 'favorite games': ['The Legend of Corgi', 'Seven Schemers', 'City Comptroller: The Fiscal Dilemma']}, 

'Jennie': {'connections': ['Levi', 'John', 'Freda', 'Robin'], 'favorite games': ['Super Mushroom Man', 'Dinosaur Diner', 'Call of Arms']}, 

'Mercedes': {'connections': ['Walter', 'Robin', 'Bryant'], 'favorite games': ['The Legend of Corgi', 'Pirates in Java Island', 'Seahorse Adventures']}, 

'John': {'connections': ['Bryant', 'Debra', 'Walter'], 'favorite games': ['The Movie: The Game', 'The Legend of Corgi', 'Dinosaur Diner']}, 

'Robin': {'connections': ['Ollie'], 'favorite games': ['Call of Arms', 'Dwarves and Swords']}, 

'Bryant': {'connections': ['Olive', 'Ollie', 'Freda', 'Mercedes'], 'favorite games': ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']},

'Walter': {'connections': ['John', 'Levi', 'Bryant'], 'favorite games': ['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man']} }

And here's my code:

def get_secondary_connections(network, user):
    if user not in network:
        return None
    sec_connections = []
    for friend in network[user]['connections']:
        for connection in network[friend]['connections']:
            if connection not in sec_connections:
                sec_connections.append(connection)
    return sec_connections

When I run get_secondary_connections(network, “Mercedes”), I get the following output:

[‘John’, ‘Levi’, ‘Bryant’]

Which, if you checked back at my network, is only Walter's list of connections. I'm supposed to get the full list of Mercedes' secondary connections, that is:

[‘John’, ‘Levi’, ‘Bryant’, ‘Ollie’, ‘Olive’, Freda’, ‘Mercedes’]

Can someone please help me?

I copy-pasted your code and run the program and got the output:

['John', 'Levi', 'Bryant', 'Ollie', 'Olive', 'Freda', 'Mercedes']

This output is what you wanted, right? There's nothing wrong with your code. If you'd like, this is how the code looks:

network = {
'Freda': {'connections': ['Olive', 'John', 'Debra'], 'favorite games': ['Starfleet Commander', 'Ninja Hamsters', 'Seahorse Adventures']},

'Ollie': {'connections': ['Mercedes', 'Freda', 'Bryant'], 'favorite games': ['Call of Arms', 'Dwarves and Swords', 'The Movie: The Game']},

'Debra': {'connections': ['Walter', 'Levi', 'Jennie', 'Robin'], 'favorite games': ['Seven Schemers', 'Pirates in Java Island', 'Dwarves and Swords']},

'Olive': {'connections': ['John', 'Ollie'], 'favorite games': ['The Legend of Corgi', 'Starfleet Commander']},

'Levi': {'connections': ['Ollie', 'John', 'Walter'], 'favorite games': ['The Legend of Corgi', 'Seven Schemers', 'City Comptroller: The Fiscal Dilemma']},

'Jennie': {'connections': ['Levi', 'John', 'Freda', 'Robin'], 'favorite games': ['Super Mushroom Man', 'Dinosaur Diner', 'Call of Arms']},

'Mercedes': {'connections': ['Walter', 'Robin', 'Bryant'], 'favorite games': ['The Legend of Corgi', 'Pirates in Java Island', 'Seahorse Adventures']},

'John': {'connections': ['Bryant', 'Debra', 'Walter'], 'favorite games': ['The Movie: The Game', 'The Legend of Corgi', 'Dinosaur Diner']},

'Robin': {'connections': ['Ollie'], 'favorite games': ['Call of Arms', 'Dwarves and Swords']},

'Bryant': {'connections': ['Olive', 'Ollie', 'Freda', 'Mercedes'], 'favorite games': ['City Comptroller: The Fiscal Dilemma', 'Super Mushroom Man']},

'Walter': {'connections': ['John', 'Levi', 'Bryant'], 'favorite games': ['Seahorse Adventures', 'Ninja Hamsters', 'Super Mushroom Man']} 
}

def get_secondary_connections(network, user):
    if user not in network:
        return None
    sec_connections = []
    for friend in network[user]['connections']:
        for connection in network[friend]['connections']:
            if connection not in sec_connections:
                sec_connections.append(connection)
    return sec_connections

print get_secondary_connections(network, "Mercedes")

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