0

Possible Duplicate:
Flatten (an irregular) list of lists in Python

Is there a way to store the results without having to write every sublist name? the numbers in every array are just examples of what I want to do. Thank you in advance.

        _store = []

        _arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        _arr3 = [1, 2, 3, _arr4, 5, 6, 7, 8, 9]
        _arr2 = [1, 2, 3, 4, 5, 6, _arr3, 8, 9]
        _arr = [1, 2, 3, _arr2, 5, 6, 7, 8, 9]

        for _value in _arr:

            #If value is a list get it, go over that list and so on
            if isinstance( _value, list ):

                _store.append(_value)
                _sub_list = _value

                if isinstance( _sub_list, list ):

                    _store.append( _sub_list)
                    _sub_sub_list = _sub_list

                    if isinstance( _sub_sub_list, list ):

                        _store.append( _sub_list)
                        _sub_sub_sub_list = _sub_list

                        #and so on...

        print _store
Community
  • 1
  • 1
Uuid
  • 2,506
  • 5
  • 28
  • 37
  • "I want to store the results" <- the results of what? What do you want to do with them later? What does "without having to write every sublist name" mean? – inspectorG4dget Nov 22 '12 at 18:37
  • Whatever you're trying to do, this weird convolution probably means you are not taking a healthy approach... – heltonbiker Nov 22 '12 at 18:37
  • If you're not using the first level `_sub_list`, you can just keep using this same name and overwriting with the newly found values. But I think the recursive approach given in an answer is best. – heltonbiker Nov 22 '12 at 18:40
  • Do you want me to copy paste all bunch of data from my database? or do you want me to expose data from my company, I think simplier questions are better then having to explain every time what I want to do, despite it really becomes clear for educative purposes. – Uuid Nov 22 '12 at 18:43
  • Desired demo output in store? – Barney Szabolcs Nov 22 '12 at 18:46

3 Answers3

3

Using recursion:

a = [1,2,3]
b = [4,a,5]
c = [6,b,7]

def add_to(input, output):
    for x in input:
        if isinstance(x, list):
            add_to(x, output)
        else:
            output.append(x)

result = []

add_to(c, result) # now result = [6, 4, 1, 2, 3, 5, 7]

The un-pythonicness of passing the output ref through the recursion can be eliminated by using a generator:

def flatten(input):
    for x in input:
        if isinstance(x, list):
            for y in flatten(x):
                yield y
        else:
            yield x

result = list(flatten(c))
cmh
  • 10,612
  • 5
  • 30
  • 40
1

Why not just serialize the list to JSON?

import json
json.dumps(arr)
onon15
  • 3,620
  • 1
  • 18
  • 22
1

You could try using recursion:

arr4 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr3 = [1, 2, 3, arr4, 5, 6, 7, 8, 9]
arr2 = [1, 2, 3, 4, 5, 6, arr3, 8, 9]
arr = [1, 2, 3, arr2, 5, 6, 7, 8, 9]

def get_store(array):
    store = []
    for item in array:
        if isinstance(item, list):
            store.extend(get_store(item))
        else:
            store.append(item)
    return store

print get_store(arr)

...which outputs:

[1, 2, 3, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 8, 9, 5, 6, 7, 8, 9]

Basically, whenever you notice that you're taking some operation and continuously nesting or repeating it, but have difficulty turning it into a for-loop, it's a good sign that you could try recursion.

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224