0

I have a multi-layered typle/list container. Manual unboxing the final data with inserted list comprehensions gives me a lot of headache.

[[(23,)],[(124,)],[(45,)]]

What is a proper way to unbox final values in simple list like this ?

[23,124,45]

I`ve already tried google, but i see only explanation whan boxi/unboxing is , but i sure that there should be some short way to do this, except inserted list comprehensions

Danylo Gurianov
  • 545
  • 2
  • 7
  • 21

3 Answers3

0
In [1]: v = [[(23,)],[(124,)],[(45,)]]

In [2]: [b[0] for b in [a[0] for a in v]]
Out[3]: [23, 124, 45]
0
srcList = [[(23,)],[(124,)],[(45,)]]
dstList = []
#--------------------------------------------------------------------
def Expand( srcList ) :
    if hasattr(srcList, '__iter__'):
        for i in srcList:
            Expand( i )
    else:
        dstList.append( srcList )
#--------------------------------------------------------------------
if __name__ == '__main__':
    Expand( srcList )
    print dstList

another similar approach likes the following code.

#--------------------------------------------------------------------
class   Expander:
    def __init__( self ):
        self.resultList = []

    def Expand( self, srcList ):
        self.resultList = []
        self.Internal_Expand( srcList )
        return self.resultList

    def Internal_Expand( self, srcList ):
        if hasattr(srcList, '__iter__'):
            for i in srcList:
                self.Internal_Expand( i )
        else:
            self.resultList.append( srcList )
#--------------------------------------------------------------------
if __name__ == '__main__':
    srcList = [[(23,)],[(124,)],[(45,)]]
    print Expander().Expand( srcList )
#--------------------------------------------------------------------

ok, finally i find this way.

#--------------------------------------------------------------------
def Expand( srcList ):
    resultList = []
    def Internal_Expand( xList ):
        if hasattr(xList, '__iter__'):
            for i in xList:
                Internal_Expand( i )
        else:
            resultList.append( xList )
    Internal_Expand( srcList )
    return resultList
#--------------------------------------------------------------------
if __name__ == '__main__':
    srcList = [[(23,)],[(124,)],[(45,)]]
    print Expand( srcList )
#--------------------------------------------------------------------
waylight
  • 1
  • 1
0

Assuming all your data are not iterables, we can use a recursive approach:

import collections

def iterable(obj):
    return isinstance(obj, collections.Iterable)

def unbox(obj):
    if iterable(obj):
        result = []
        for x in obj:
            result.extend(unbox(x))
        return result
    else:
        return [obj]

This approach can be converted into a sequential function if needed:

from collections import deque

def unbox(obj):
    obj_list = deque([obj])
    result = []
    while(len(obj_list) > 0):
        current = obj_list.popleft()
        if iterable(current):
            obj_list.extend(current)
        else:
            result.append(current)
    return result
Samy Arous
  • 6,794
  • 13
  • 20