Reputation: 33
I'm successfully returning an array that maps out the ideal path between nodes on a network.
The result is (for a sample i,j pair) something like this.
1>34>65>23>742
Now I'd like to replace the numbers with the name of the city it represents, like San Francisco (SFO), so it would read something like
SFO>LAX>DFW>JFK
I've tried using the replace functions, but to no success.
for z in range(len(names)):
nextstop = [sub.replace(z, names[z]) for sub in nextstop]
Where names
contains the names of the airports, in the same order/index as the numbers.
Thanks!
where the numbers represent
Upvotes: 1
Views: 91
Reputation: 3583
Try something like this:
cities = [names[int(x)] for x in "1>34>65>23>742".split(">")]
">".join(cities)
What's happening is that "1>34>65>23>742".split(">")
gives you a list of the numbers as strings. Then the list comprehension [names[int(x)] for x in "1>34>65>23>742".split(">")]
takes each of those strings, turns them into numbers and looks them up in names
. So now we have a list of numbers. Finally "<".join
turns them back into your string format again.
Upvotes: 1