3

I'm having trouble getting my head around a problem on recursive nested lists. The problem; need to define a procedure to access a nested list to an arbitrary depth. It would take an nested list and an index, and return the part of the list at that index. From this given function, recursively find the value at the given index.

For example

Well here is a better visual representation. To select the element 9 out of it, we need to do something like nested[3][1]

    nested = \
    [[[1, 2],
      3],
     [4,
      [5, 6]],
     7,
     [8, 9, 10]]

recursive_func(nested_list, [3,1]) #recursive function definition, the second argument is the index at which the data needs to be retrieved.  
>>> 9 #so given the function an index of [3,1] would return 9

Any help to point me in the right direction would be grateful

Benji
  • 591
  • 1
  • 4
  • 11

2 Answers2

1

This may do you, but I'm still not 100% sure what you are looking for...

>>> def findItem(nested, pos):
    if pos[0] == 1:
        return nested[pos[1]-1]
    else:
        nextLevelDown = []
        for item in nested:
            if type(item) == type([]):
                nextLevelDown = nextLevelDown + item
        return findItem(nextLevelDown, [pos[0]-1, pos[1]])

>>> findItem([[[1, 2], 3], 4], [3, 1])
1
>>> findItem([[[1, 2], [3]], 4], [3, 3])
3
>>> findItem([[[1, 2], [3]], 4], [2, 2])
[3]

UPDATE: So after much back and forth, I finally understand the question, and it is much simpler than it originally seemed, all you need is:

>>> def recursiveRef(nested, idxList):
    if len(idxList) > 1:
        return recursiveRef(nested[idxList[0]], idxList[1:])
    return nested[idxList[0]] 

>>> recursiveRef([[[1, 2], 3], [4, [5, 6]], 7, [8, 9, 10]], [3, 1])
9
verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • @veredesmarald i just tried it findItem([[[1, 2], 3], [4, [5, 6]], 7, [8, 9, 10]], [3, 1]) >>> 1 should be 9. Thanks for the attempt i'm starting to understand how to attempt these kinds of problems now – Benji May 10 '11 at 08:43
  • @Ben E: How did you arrive at 9? The first element at depth 3 in that list is indeed 1. 9 isn't even 3 levels deep, only 2... I would have expect the index of 9 to be [2, 6], or possibly [2, 4] if you only count the integers at a certain depth and not the lists... – verdesmarald May 10 '11 at 08:47
  • is kinda hard to explain. so 3 deep is [8, 9, 10] and 1 deep into [8, 9, 10] = 9 – Benji May 10 '11 at 08:50
  • @Ben E: Sorry, but I have great difficulty understanding your co-ordinate system... How is [8,9,10] 3 levels deep when it is nested directly inside the outermost list? Unless you can more fully explain the meaning of the two values in the position I don't think I can help any further... – verdesmarald May 10 '11 at 08:57
  • 'nested = \ [[[1, 2], 3], [4, [5, 6]], 7, [8, 9, 10]]' Well here is a better visual representation. To select the element 9 out of it, we need to do something like nested[3][1] – Benji May 10 '11 at 09:03
  • tags don't work in comments, so that didnt help much. :( Try adding the additional example to the original question. – verdesmarald May 10 '11 at 09:05
  • @veredesmarald i updated that post please check, also thanks for the help so far much appreciated. I find that my HW questions are worded badly so it makes it harder to understand. – Benji May 10 '11 at 09:11
  • @Ben E: See my updated answer. In the future you should make sure your examples are correct the first time around to save many headaches! :) – verdesmarald May 10 '11 at 09:26
  • @verdesmarald Sorry about that kinda had a big day and didn't proof read. Thanks for your help in understand the problem – Benji May 10 '11 at 09:30
0

If I understand this correctly, you want simply turn nested list into non-nested? And then check the index? If so, then you can try something like this (I don't have python at the moment, so treat it like a pseudocode):

def unwrap_list(list, result):
   if type(list) == type([]):
      for value in list:
         unwrap_list(value, result)
   else:
      result.append(list)

Make sure, to define variable unwrapped = [] before calling this function and use unwrap_list(list, unwrapped). The result will be stored in variable unwrapped.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • I'm not sure how this generates the answer the OP asks for ? (namely, `1` for the given input) – Eli Bendersky May 10 '11 at 07:45
  • Well, if I understand correctly, then first you call `unwrap_list(nested_list, result)` and then just check `result[index]`. I don't really understand the second argument in `recursive_func(nested_list, [3,1])` (I assumed, that it is a mistake and it should be `recursive_func(nested_list, 1)`) but what I wrote is a hint for doing recursion in this case. – freakish May 10 '11 at 07:48
  • The second argument is a index into the nested list. Its the position of were the data i want to retrieve. so at position [3,1] would return the integer of 1 – Benji May 10 '11 at 07:58
  • @Ben E: why is `[3, 1]` a position? it's a list. can you explain what it means? – Eli Bendersky May 10 '11 at 08:12
  • Well position as in being an index into the list. So for example n = [1,2,3] to access 2 it would use the index 1 (n[1]) – Benji May 10 '11 at 08:20