Can anyone tell me how to slice through a list of varying types to include only integers?
A list such as this:
c = [32.45, 'foo', 3, 4, 'fare', True, 3.9871]
That would be
c = [32.45, 'foo', 3, 4, 'fare', True, 3.9871]
print (c)
d = [val for val in c if type(val) is int]
print (d)
Result:
[32.45, 'foo', 3, 4, 'fare', True, 3.9871]
[3, 4]
You need type
here, not isinstance
because that includes True
.
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.