使用python中的列表理解将嵌套列表转换为普通列表 [英] convert nested list to normal list using list comprehension in python

查看:408
本文介绍了使用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]

这篇关于使用python中的列表理解将嵌套列表转换为普通列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆