Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从列表中删除空值_Python - Fatal编程技术网

在Python中从列表中删除空值

在Python中从列表中删除空值,python,Python,我正在尝试从下面的列表中删除空值,但无法执行此操作。请帮忙 >>> xx [[], [], [], [], [], [], ['5'], [], [], [], [], []] >>> type(xx) <type 'list'> >>xx [[], [], [], [], [], [], ['5'], [], [], [], [], []] >>>类型(xx) 只需创建没有不需要的值的列表(没有空列表) 列表理解 xx = [el for e

我正在尝试从下面的列表中删除空值,但无法执行此操作。请帮忙

>>> xx
[[], [], [], [], [], [], ['5'], [], [], [], [], []]

>>> type(xx)
<type 'list'>
>>xx
[[], [], [], [], [], [], ['5'], [], [], [], [], []]
>>>类型(xx)

只需创建没有不需要的值的列表(没有空列表)

列表理解

xx = [el for el in xx if el]
使用
filter()
lambda

xx = filter(lambda x: x, xx)

请尝试下面的代码

xx = [[], [], [], [], [], [], ['5'], [], [], [], [], []]
yy = []
def remove_if_null(xx):
   for i in xx:
       if i:
           yy.append(i)

remove_if_null(xx)

xx = yy
print xx
print yy

问题是什么?显示type和dir的结果有什么意义呢?那么您在哪里尝试删除空值呢?抱歉@DanielRoseman,这是意外出现的。我已经删除了它。@AnandSKumar,我已经尝试过这个方法,而True:try:xx。remove(“”)除外ValueError:break
xx=filter(bool,xx)
更简单。谢谢。这确实有效,但你能解释一下它到底在做什么吗。我知道这是切片,但还是需要一个简单的解释helpful@fear_matrix这只是简单的列表理解condition@ojii小心点。bool(0)为False,但0不是None。如果你的唯一目标是不过滤,不要抄近路。这不是一个好的解决方案。。。user3990145的答案是正确的。