使用python中的列表理解将嵌套列表转换为普通列表
问题描述:
如何在Python中执行以下操作?
How can I do the following in Python?
a = [2,[33,4],[2,3,4,6]]
li = [ i for i in a if isinstance(i,int) else j in i ]
我如何将列表a转换为= [2,33,4,2,3,4,6]
how do i convert list a into a = [2,33,4,2,3,4,6]
我能够使用普通的for循环来做到这一点,但我只想使用列表理解
I am able to do it with normal for loop but i want to use only list comprehension
答
您可以使用:
In [20]: [k for e in a for k in (e if isinstance(e, list) else [e])]
...:
Out[20]: [2, 33, 4, 2, 3, 4, 6]