dech
dech

Reputation: 11

Printing the items from a list with the index value of another list?

I have two lists, one with destinations and one with the cost you would need to go to said place.

What I need to do is for example, if the user chooses "7500" all of the places that cost "7500" will show. As you can see in the code below "7500" is repeated twice, so I would need the two destinations that have that price.

I already have the code to find the points needed, but I have no idea on how to continue with printing the destinations that go with those points.

destinations = ["Toronto", "Winnipeg", "London", "Ottawa","Miami", "Edmonton"]
pointCosts = [7500, 9000, 11000, 7500, 9500, 9000]

def CheapPoint (pointCosts):
    lowest = [0]
    for x in pointCosts:
        if x < lowest:
            lowest = x

For example, for output I would like something like:

Points: 7500 City: Toronto City: Ottawa

As of right now I only get the points, but I would also like to get the destinations, also I cannot use any built-in functions.

Thanks

Upvotes: 0

Views: 51

Answers (3)

Amit Gupta
Amit Gupta

Reputation: 2928

[ print(y,x) for x, y in zip(destinations, pointCosts) if y == 7500]

7500, 'Toronto',

7500, 'Ottawa'

Upvotes: 0

Andrea Grioni
Andrea Grioni

Reputation: 187

score = 7500

example = [ x for x, y in zip(destinations, pointCosts) if y == score ]

output

['Toronto', 'Ottawa']

you can print on separate lines by print() function:

print(*example, sep = '\n')

output:

Toronto
Ottawa

Upvotes: 2

solarflare
solarflare

Reputation: 1092

Have you tried using a dictionary instead of two separate lists? e.g:
locations = {"Toronto":7500, "Winnipeg":9000 ...}

points = 7500
for destination,pointCost in locations.items():
    if pointCost == points:
        print (destination)

You can then iterate through the dictionary to pull out aggregations of keys

Upvotes: 0

Related Questions