1

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

1
  • I don't know the numbers representing cities but i suggest to use re.sub method from re module Commented Oct 15, 2020 at 22:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.