Convert list of tuples to list?

后端 未结 11 1151
旧巷少年郎
旧巷少年郎 2020-12-02 12:54

How do I convert

[(1,), (2,), (3,)]

to

[1, 2, 3]
11条回答
  •  悲哀的现实
    2020-12-02 13:31

    >>> a = [(1,), (2,), (3,)]
    >>> b = map(lambda x: x[0], a)
    >>> b
    [1, 2, 3]
    

    With python3, you have to put the list(..) function to the output of map(..), i.e.

    b = list(map(lambda x: x[0], a))
    

    This is the best solution in one line using python built-in functions.

提交回复
热议问题