简体   繁体   中英

how to detect logical and string index in sub-list and delete it

I have a list like this:

A = [[1,2,3,4],[1,1,2,4],[1,2,3,False],[1,False,2,3],[1,2,3,4],[1,2,3,'word'],[5,6,7,8],[1,4,3,4],[True,1,2,4],[0,1,0,1],[0,0,0,0],[False,False,False,False]]

and I want as output a list like this:

A = [[1,2,3,4],[1,1,2,4],[1,2,3,4],[5,6,7,8],[1,4,3,4],[0,1,0,1],[0,0,0,0]]

I just want to delete or remove a any list. it have member of string or logical. how i can do it.

We can do this with list comprehension where we perform a filter with any(..) that checks if there is any element that is an instance of str or bool :

[sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]

this then yields:

>>> [sublist for sublist in A if not any(isinstance(e, (str, bool)) for e in sublist)]
[[1, 2, 3, 4], [1, 1, 2, 4], [1, 2, 3, 4], [5, 6, 7, 8], [1, 4, 3, 4], [0, 1, 0, 1], [0, 0, 0, 0]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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