0

I have this list and a start point as below:

lst = [[0, 1], [0, 4], [3, 4], [5, 1], [5, 2]]
point = 3 

I want to write a function that gives me this list in return: [3,4,0,1,5,2]. It takes start point, finds the pair containing the start point and takes its other half as the new start point.

Given list is an example, length of the list is not specified.

I tried to write a for function but, since the length of the list is not constant, it doesn't give the correct output

What I tried:

  def func(start,lst):
      for i in range(len(lst)):
          if lst[i][1]==start:
              cont==lst[i][0]
              lst2=lst.remove(lst[i])
          if lst[i][0]==start:
              cont==lst[i][1]
              lst2=lst.remove(lst[i])
      func(cont,lst2)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3142764
  • 55
  • 1
  • 1
  • 6
  • 3
    I don't understand the algorithm (can't make it run in my head), can you explain with a complete example? – Maxime Lorant Dec 28 '13 at 21:56
  • 2
    You're making the same error as in [your other question from today](http://stackoverflow.com/questions/20819504/python-list-index-out-of-range-iteration). Don't remove elements from a list you're iterating over! – Tim Pietzcker Dec 28 '13 at 21:58
  • The code indention is messed-up, please correct if it doesn't look like this in your original code. –  Dec 28 '13 at 22:00
  • 1
    Also lst2=lst.remove(lst[i]) will assign None to lst2, since list.remove function returns nothing, only makes changes to the list it was called from. – Arpit Singh Dec 28 '13 at 22:00
  • @MaximeLorant My understanding is: `lst` represents the (unordered list of) edges of a linear graph (path) and this should be transformed into the list of nodes in correct adjacency order. –  Dec 28 '13 at 22:03
  • i'd like to order the list, 3 is the start point, 4 is the other point of lst[2], 0 is the point near 4 and 1 is point near 0... it goes like this – user3142764 Dec 28 '13 at 22:04
  • @Nabla Aaaah, correct. Good eye. – Maxime Lorant Dec 28 '13 at 22:04

2 Answers2

0

How about this (without using recursion):

def func(lst, start):
    result = []
    while True:
        found = False
        for item in lst:
            if start in item:
                item.remove(start)
                result.append(start)
                found = True
                if item:
                    start = item[0]
                    item.remove(start)
                    break
        if not found:
            result.append(start)
            break
    return result

Usage:

>>> func([[0, 1], [0, 4], [3, 4], [5, 1], [5, 2]], 3)
[3, 4, 0, 1, 5, 2]
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

You need something like:

def func(start, lst, out=None):
    if out is None: # create new output list on first call
        out = []
    out.append(start) # add the starting value
    if len(lst) == 0: # base case
        return out
    for i in range(len(lst)):
        if start in lst[i]: # find the node
            item = lst.pop(i) # remove the node
            next_ = item[0] if item[1] == start else item[1] # get next 'start'
            return func(next_, lst, out) # recurse

I get:

>>> func(3, [[0, 1], [0, 4], [3, 4], [5, 1], [5, 2]])
[3, 4, 0, 1, 5, 2]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437