简体   繁体   中英

How to remove strings from a list of list of integers?

I have a list of lists of integers and strings and I want to remove the strings. How would I go about doing this?

a = [[1,2,3,4,5,'test'], [6,7,84,3,2,4,'nan'], [4,1,2,4,5,42,4,'test']]

I have tried the following

for i in a:
for e in i:
    if isinstanece(e, str) == True:
        a.remove(e)

Nested list comprehensions will achieve what you're looking for.

[[val for val in lst if isinstance(val,int)] for lst in a]

edit: You could also check that the value is not a string:

[[val for val in lst if not isinstance(val,str)] for lst in a]

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