mardi 19 avril 2016

How to nest 2 for loops into one if statement

How do you put 2 for loops inside an if statement?

Ships = [
    ["Aircraft Carrier", 5], 
    ["Battleship", 4], 
    ["Submarine", 3],    
    ["Destroyer", 3], 
    ["Patrol Boat", 2]
]
ships_left = ["A","B","S","D","P"]

if [ship for ship in ships_left name for name in Ships if name[0][0] == ship]:
    print(name[0])

Expected output:

Aircraft Carrier

This is because if they both iterate once then ship should be equal to "A" and name should be ["Aircraft Carrier", 5], so name[0][0] should be "A".

How would you do this for it to actually work?

Aucun commentaire:

Enregistrer un commentaire