atcoder tutorial There was a problematic one that had to determine whether each element of the list was even or odd, so I usually had a built-in function when I thought it would be easy to write. It may be a story to write that much, but please let me use it because it is prepared.
all(iterable)
Returns True if all ** elements of iterable are true ** (or if iterable is empty).
def all(iterable):
for element in iterable:
if not element:
return False
return True
Here's what ** itarable ** is.
A repeatable object. An object that returns one element at a time. ex) Lists, tuples, dictionaries ...
Passing this as an argument to ** iter () ** will return ** iterator ** for the object. (** iterator ** is an object that expresses the flow of data (roughly))
For example
for i in A:
A here is ** itarable **. for
automatically calls ** iter () ** to create a temporary anonymous variable and keep the ** iterator ** while looping.
So let's try ʻall`!
The following is a program that checks if all the elements of the array ** array ** using ʻall` are int type. Think about what it looks like before you look at the output!
def check(array):
if all( isinstance(x, int) for x in array):
print("Array{}All elements of are int type".format(array))
else:
print("Array{}Some of the elements are not int type! !!".format(array))
check([2,5,6])
check([1,4,[]])
check(["1",2,3])
check([])
check(1)
All elements of array [2, 5, 6] are int type Some elements of the array [1, 4, []] are not of type int! !! Some elements of the array ['1', 2, 3] are not of type int! !! All elements of array [] are int type
TypeError Traceback (most recent call last)
Omitted
TypeError: 'int' object is not iterable
Was it as expected? ** True ** is also returned for empty arrays. Well, of course, I got an error saying that the fifth one is not iterable. Keep these points in mind.
any(iterable)
Next, I will write about ʻany ()`. It's a little late, but there seems to be no demand for explanation because the contents of these two functions are still the function names (laughs).
Returns True if any ** element of iterable is true **. Returns False if iterable is empty. Equivalent to the following code:
def any(iterable):
for element in iterable:
if element:
return True
return False
The following is a program that checks if an element of an array ** array ** using ʻany` has an int type. As before, think about what it will look like before you look at the output!
def check(array):
if any( isinstance(x, int) for x in array):
print("Array{}There is an int type in the element of! !!".format(array))
else:
print("Array{}There is no int type in the element of!".format(array))
check([2,"5","6"])
check(["1","4","a"])
check([])
check([1])
The element of the array [2, '5', '6'] has an int type! !! There is no int type in the element of the array ['1', '4','a']! There is no int type in the element of array []! Int type is in the element of array [1]! !!
how was it? No. 3 is especially careful. You can see that False
is returned in the case of an empty array, unlike ʻall`. Well, is there anything else I can explain?
What did you think? I can't deny the commentary and the feeling that there was no demand in the first place, but I hope it helps. Then this time around here!
Recommended Posts