繁体   English   中英

如何在Python中查找列表中所有为空的索引

[英]How to find all indexes of a list that are empty in Python

我想知道是否有一种简单的方法来查找列表中所有为空的索引。

mylist  = [ [1,2,3,4], [] , [1,2,3,4] , [] ]

next((i for i, j in enumerate(mylist) if not j),"no empty indexes found")将返回列表的第一个空索引,但是我可以返回所有索引吗? 如果"no empty indexes found" ,它将默认为字符串"no empty indexes found"

我想将所有空索引附加到另一个列表以使用。

my_indexes_list.append(next((i for i, j in enumerate(self.list_of_colors) if not j),5))

使用以下事实:布尔序列中的空序列为假,以及列表理解:

>>> mylist  = [[1,2,3,4], [] , [1,2,3,4] , []]
>>> [i for i,x in enumerate(mylist) if not x]
[1, 3]
>>> [i for i,x in enumerate(mylist) if x ==[] ]
[1, 3]
empties[]
for s in mylist:
    is len(s) == 0
        empties.append(mylist.index(s))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM