python list comprehensions; compressing a list of lists?

后端 未结 13 1991
名媛妹妹
名媛妹妹 2020-11-30 01:49

guys. I\'m trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I\'m trying to do.

What I\'m doing is this. I

13条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 02:20

    >>> from functools import reduce
    >>> listOfLists = [[1, 2],[3, 4, 5], [6]]
    >>> reduce(list.__add__, listOfLists)
    [1, 2, 3, 4, 5, 6]
    

    I'm guessing the itertools solution is more efficient than this, but this feel very pythonic.

    In Python 2 it avoids having to import a library just for the sake of a single list operation (since reduce is a built-in).

提交回复
热议问题