So I tried to do something like list.is_integer()
but I don't think it's working.
Is there any method to determine if a list
has for example 2 whole numbers in it?
If you know the number, simply use the in
operator:
1 in [1, 2, 3]
# True
if you want to check the whole list consisting of integers, you'll need to use something different, for example:
all(isinstance(item, int) for item in [1, 2, 3])
# True
all()
helps evaluating all of the values of an iterable and the condition for all items of a list being of int
type can be checked by isinstance()
function.
For specific count of the occurrences, simply switch all()
to sum()
which will add all of the truthy values that fulfill the condition:
sum(isinstance(item, int) for item in [1, 2, "3"])
# 2
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.