flatten list of list through list comprehension

前端 未结 5 725
闹比i
闹比i 2020-12-01 21:15

I am trying to flatten a list using list comprehension in python. My list is somewhat like

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

just for printing

相关标签:
5条回答
  • 2020-12-01 21:37

    An alternative solution using a generator:

    import collections
    
    def flatten(iterable):
        for item in iterable:
            if isinstance(item, collections.Iterable) and not isinstance(item, str):  # `basestring` < 3.x
                yield from item  # `for subitem in item: yield item` < 3.3
            else:
                yield item
    
    >>> list(flatten([[1, 2, 3], [4, 5, 6], 7, 8]))
    [1, 2, 3, 4, 5, 6, 7, 8]
    
    0 讨论(0)
  • 2020-12-01 21:44

    No-one has given the usual answer:

    def flat(l):
      return [y for x in l for y in x]
    

    There are dupes of this question floating around StackOverflow.

    0 讨论(0)
  • 2020-12-01 21:48

    You're trying to iterate through a number, which you can't do (hence the error).

    If you're using python 2.7:

    >>> from compiler.ast import flatten
    >>> flatten(l)
    [1, 2, 3, 4, 5, 6, 7, 8]
    

    But do note that the module is now deprecated, and no longer exists in Python 3

    0 讨论(0)
  • 2020-12-01 21:51
    def nnl(nl):    # non nested list
    
        nn = []
    
        for x in nl:
            if type(x) == type(5):
                nn.append(x)
    
        if type(x) == type([]):
            n = nnl(x)
    
            for y in n:
                nn.append(y)
        return nn
    
    print (nnl([[9, 4, 5], [3, 8,[5]], 6]))  # output: [9, 4, 5, 3, 8, 5, 6]
    
    0 讨论(0)
  • 2020-12-01 21:56
    >>> from collections import Iterable
    >>> from itertools import chain
    

    One-liner:

    >>> list(chain.from_iterable(item if isinstance(item,Iterable) and
                        not isinstance(item, basestring) else [item] for item in lis))
    [1, 2, 3, 4, 5, 6, 7, 8]
    

    A readable version:

    >>> def func(x):                                         #use `str` in py3.x 
    ...     if isinstance(x, Iterable) and not isinstance(x, basestring): 
    ...         return x
    ...     return [x]
    ... 
    >>> list(chain.from_iterable(func(x) for x in lis))
    [1, 2, 3, 4, 5, 6, 7, 8]
    #works for strings as well
    >>> lis = [[1, 2, 3], [4, 5, 6], 7, 8, "foobar"]
    >>> list(chain.from_iterable(func(x) for x in lis))                                                                
    [1, 2, 3, 4, 5, 6, 7, 8, 'foobar']
    

    Using nested list comprehension:(Going to be slow compared to itertools.chain):

    >>> [ele for item in (func(x) for x in lis) for ele in item]
    [1, 2, 3, 4, 5, 6, 7, 8, 'foobar']
    
    0 讨论(0)
提交回复
热议问题