1
class Map():

    def returnWay(self, node1, node2):
        final_list = []
        temp_list = []
        self._returnWay(node1, node2, final_list, temp_list)
        return final_list

    def _returnWay(self, node1, node2, final_list, temp_list):
        if node1 not in temp_list:
            temp_list.append(node1)
            if node1 == node2:
                final_list.append(temp_list)
                del temp_list[-1]
            else:
                for x in node1.nexts():
                    self._returnWay(x, node2, final_list, temp_list)
                del temp_list[-1]


path = Map()

for x in path.returnWay(node1, node2):
    print x

Ok guys, first than anything I don't speak english really, so excuse me if I make some mistakes speaking...

Here I'm trying to get all the existing ways between two nodes, there are 4 of them, but instead I'm getting 4 empty lists.

If I put in line 13 "for x in temp_list: print x" It prints all the 4 ways, but for some reason It doesn't add them to final_list.

7
  • Plase, let me know if I'm not clear enough or I could bring you some more information. Commented Jun 5, 2016 at 8:03
  • 1
    When you get back to the initial node (i.e. node1 == node2) you call del temp_list[-1] twice, once before returning and once after returning. I'm not so sure how your code is supposed to work, but this is the first think that strikes me. Commented Jun 5, 2016 at 8:04
  • Thanks for answering. First del is there because when node1 and node2 coincide there's no more reason to go through that way. And the second one happen when all the adyacents nodes have been visited, so It could watch for former ways. Commented Jun 5, 2016 at 8:14
  • 1
    So node.nexts() returns all the successor nodes of node? Is the graph directed? Commented Jun 5, 2016 at 8:16
  • 1
    The first thing I'd try is final_list.append(temp_list[:]). Commented Jun 5, 2016 at 8:18

1 Answer 1

1

your code cannot run on my computer, because node1 is not defined.

But I think I found the problem: In Python, if you append temp_list to final_list, all changes you do on temp_list apply also to final_list.

I tried this out in the terminal, look here:

>>> a = ['a', 'b', 'c']
>>> e = 'd'
>>> a.append(e)
>>> a
['a', 'b', 'c', 'd']
>>> flist=[]
>>> flist.append(a)
>>> flist
[['a', 'b', 'c', 'd']]
>>> del a[-1]
>>> a
['a', 'b', 'c']
>>> flist
[['a', 'b', 'c']]

a solution would be to create a full list copy and put this into your final list. How is a full list copy created on the fly? temp_list[:] So here my solution:

class Map():

    def returnWay(self, node1, node2):
        final_list = []
        temp_list = []
        self._returnWay(node1, node2, final_list, temp_list)
        return final_list

    def _returnWay(self, node1, node2, final_list, temp_list):
        if node1 not in temp_list:
            temp_list.append(node1)
            if node1 == node2:
                final_list.append(temp_list[:])
                del temp_list[-1]
            else:
                for x in node1.nexts():
                    self._returnWay(x, node2, final_list, temp_list[:])
                del temp_list[-1]


path = Map()

for x in path.returnWay(node1, node2):
    print x

so I hope this should work as you want it.

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

2 Comments

Your answers are really useful for me, thank you very much for your time, this was what I was waiting for.
I hope I my correction was correct. As I mentioned above, I cannot run the code on my computer, since not enough information is given, so I have no possibilities to check ...

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.