簡體   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