lzc*_*lzc 3 python iterator enumeration list
我想知道是否有一种简单的方法可以找到列表的所有空索引。
mylist = [ [1,2,3,4], [] , [1,2,3,4] , [] ]
Run Code Online (Sandbox Code Playgroud)
next((i for i, j in enumerate(mylist) if not j),"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))
Run Code Online (Sandbox Code Playgroud)
使用布尔上下文中空序列为假的事实,以及列表推导式:
>>> mylist = [[1,2,3,4], [] , [1,2,3,4] , []]
>>> [i for i,x in enumerate(mylist) if not x]
[1, 3]
Run Code Online (Sandbox Code Playgroud)