I have a list with empty lists in it:
我有一个包含空列表的列表:
list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
How can I remove the empty lists so that I get:
如何删除空列表以便我得到:
list2 = ['text', 'text2', 'moreText']
I tried list.remove('') but that doesn't work.
我尝试了list.remove(''),但这不起作用。
9 个解决方案
#1
54
Try
尝试
list2 = [x for x in list1 if x != []]
If you want to get rid of everything that is "falsy", e.g. empty strings, empty tuples, zeros, you could also use
如果你想摆脱所有“麻痹”的东西,例如空字符串,空元组,零,你也可以使用
list2 = [x for x in list1 if x]
#2
44
You can use filter()
instead of a list comprehension:
您可以使用filter()而不是列表推导:
list2 = filter(None, list1)
If None
is used as first argument to filter()
, it filters out every value in the given list, which is False
in a boolean context. This includes empty lists.
如果None用作filter()的第一个参数,它会过滤掉给定列表中的每个值,在布尔上下文中为False。这包括空列表。
It might be slightly faster than the list comprehension, because it only executes a single function in Python, the rest is done in C.
它可能比列表理解稍快,因为它只在Python中执行单个函数,其余的在C中完成。
#3
5
Calling filter
with None
will filter out all falsey values from the list (which an empty list is)
使用None调用过滤器将过滤掉列表中的所有falsey值(空列表为)
list2 = filter(None, list1)
#4
3
>>> list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
>>> list2 = [e for e in list1 if e]
>>> list2
['text', 'text2', 'moreText']
#5
1
I found this question because I wanted to do the same as the OP. I would like to add the following observation:
我发现了这个问题,因为我想和OP一样。我想补充以下观察:
The iterative way (user225312, Sven Marnach):
迭代方式(user225312,Sven Marnach):
list2 = [x for x in list1 if x]
Will return a list
object in python3
and python2
. Instead the filter way (lunaryorn, Imran) will differently behave over versions:
将在python3和python2中返回一个列表对象。相反,过滤方式(lunaryorn,Imran)将在版本上表现不同:
list2 = filter(None, list1)
It will return a filter
object in python3
and a list
in python2
(see this question found at the same time). This is a slight difference but it must be take in account when developing compatible scripts.
它将返回python3中的过滤器对象和python2中的列表(同时查看此问题)。这是一个细微的差别,但在开发兼容脚本时必须考虑到这一点。
This does not make any assumption about performances of those solutions. Anyway the filter object can be reverted to a list using:
这不会对这些解决方案的性能做出任何假设。无论如何,过滤器对象可以使用以下方式恢复为列表:
list3 = list(list2)
#6
0
A few options:
一些选择:
filter(lambda x: len(x) > 0, list1) # Doesn't work with number types
filter(None, list1) # Filters out int(0)
filter(lambda x: x==0 or x, list1) # Retains int(0)
sample session:
样本会话:
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
>>> filter(lambda x: len(x) > 0, list1)
['text', 'text2', 'moreText']
>>> list2 = [[], [], [], [], [], 'text', 'text2', [], 'moreText', 0.5, 1, -1, 0]
>>> filter(lambda x: x==0 or x, list2)
['text', 'text2', 'moreText', 0.5, 1, -1, 0]
>>> filter(None, list2)
['text', 'text2', 'moreText', 0.5, 1, -1]
>>>
#7
0
I needed to filter empty items from more complex nested objects. I made a recursive function to do it. See my SO answer here.
我需要从更复杂的嵌套对象中过滤掉空项。我做了一个递归函数来做到这一点。在这里看到我的答案。
#8
0
a = [[1,'aa',3,12,'a','b','c','s'],[],[],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]
b=[]
for lng in range(len(a)):
if(len(a[lng])>=1):b.append(a[lng])
a=b
print(a)
[[1,'aa',3,12,'a','b','c','s'],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]
[[1, 'AA',3,12, 'A', 'B', 'C', 'S'],[1, 'AA',7,80, 'd', 'G','F ”, ''],[9,无,11,12,13,14,15,数k']]
#9
-2
list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
list2 = []
for item in list1:
if item!=[]:
list2.append(item)
print(list2)
output:
输出:
['text', 'text2', 'moreText']
#1
54
Try
尝试
list2 = [x for x in list1 if x != []]
If you want to get rid of everything that is "falsy", e.g. empty strings, empty tuples, zeros, you could also use
如果你想摆脱所有“麻痹”的东西,例如空字符串,空元组,零,你也可以使用
list2 = [x for x in list1 if x]
#2
44
You can use filter()
instead of a list comprehension:
您可以使用filter()而不是列表推导:
list2 = filter(None, list1)
If None
is used as first argument to filter()
, it filters out every value in the given list, which is False
in a boolean context. This includes empty lists.
如果None用作filter()的第一个参数,它会过滤掉给定列表中的每个值,在布尔上下文中为False。这包括空列表。
It might be slightly faster than the list comprehension, because it only executes a single function in Python, the rest is done in C.
它可能比列表理解稍快,因为它只在Python中执行单个函数,其余的在C中完成。
#3
5
Calling filter
with None
will filter out all falsey values from the list (which an empty list is)
使用None调用过滤器将过滤掉列表中的所有falsey值(空列表为)
list2 = filter(None, list1)
#4
3
>>> list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
>>> list2 = [e for e in list1 if e]
>>> list2
['text', 'text2', 'moreText']
#5
1
I found this question because I wanted to do the same as the OP. I would like to add the following observation:
我发现了这个问题,因为我想和OP一样。我想补充以下观察:
The iterative way (user225312, Sven Marnach):
迭代方式(user225312,Sven Marnach):
list2 = [x for x in list1 if x]
Will return a list
object in python3
and python2
. Instead the filter way (lunaryorn, Imran) will differently behave over versions:
将在python3和python2中返回一个列表对象。相反,过滤方式(lunaryorn,Imran)将在版本上表现不同:
list2 = filter(None, list1)
It will return a filter
object in python3
and a list
in python2
(see this question found at the same time). This is a slight difference but it must be take in account when developing compatible scripts.
它将返回python3中的过滤器对象和python2中的列表(同时查看此问题)。这是一个细微的差别,但在开发兼容脚本时必须考虑到这一点。
This does not make any assumption about performances of those solutions. Anyway the filter object can be reverted to a list using:
这不会对这些解决方案的性能做出任何假设。无论如何,过滤器对象可以使用以下方式恢复为列表:
list3 = list(list2)
#6
0
A few options:
一些选择:
filter(lambda x: len(x) > 0, list1) # Doesn't work with number types
filter(None, list1) # Filters out int(0)
filter(lambda x: x==0 or x, list1) # Retains int(0)
sample session:
样本会话:
Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
>>> filter(lambda x: len(x) > 0, list1)
['text', 'text2', 'moreText']
>>> list2 = [[], [], [], [], [], 'text', 'text2', [], 'moreText', 0.5, 1, -1, 0]
>>> filter(lambda x: x==0 or x, list2)
['text', 'text2', 'moreText', 0.5, 1, -1, 0]
>>> filter(None, list2)
['text', 'text2', 'moreText', 0.5, 1, -1]
>>>
#7
0
I needed to filter empty items from more complex nested objects. I made a recursive function to do it. See my SO answer here.
我需要从更复杂的嵌套对象中过滤掉空项。我做了一个递归函数来做到这一点。在这里看到我的答案。
#8
0
a = [[1,'aa',3,12,'a','b','c','s'],[],[],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]
b=[]
for lng in range(len(a)):
if(len(a[lng])>=1):b.append(a[lng])
a=b
print(a)
[[1,'aa',3,12,'a','b','c','s'],[1,'aa',7,80,'d','g','f',''],[9,None,11,12,13,14,15,'k']]
[[1, 'AA',3,12, 'A', 'B', 'C', 'S'],[1, 'AA',7,80, 'd', 'G','F ”, ''],[9,无,11,12,13,14,15,数k']]
#9
-2
list1 = [[], [], [], [], [], 'text', 'text2', [], 'moreText']
list2 = []
for item in list1:
if item!=[]:
list2.append(item)
print(list2)
output:
输出:
['text', 'text2', 'moreText']