+ 2

Any() or all()

Iteration over the array with the definition of the parity of the number. The attachment is a simple option. How can I do this using any () or all () functions ? --- Перебор массива с определением четности числа. Во вложении простой вариант. Как это сделать с помощью функций any() или all()? https://code.sololearn.com/cq57LFb3AEwp/?ref=app

20th Sep 2020, 1:25 PM
Petr
11 Antworten
+ 6
here is a code snippet that you can use to see how it works. Input is given as space separated numbers. They will be split and converted to int and then stored in a list. This is the base for any() or all(). inp = [int(i) for i in input('enter some numbers sep. by space: ').split()] if all(j % 2 == 0 for j in inp): print('all numbers are Even') else: print('not all numbers are Even')
20th Sep 2020, 3:36 PM
Lothar
Lothar - avatar
+ 7
print("NO" if any(filter(lambda k:k%2, b)) else "YES")
20th Sep 2020, 10:53 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 5
print("even" if any(i%2 for i in [2,4,6,8,0,2,4,8,0]) else "odd")
21st Sep 2020, 6:13 AM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 2
Your code doesn't use an array (or list), the loop is running in range. Did you attach the right code link?
20th Sep 2020, 1:31 PM
Ipang
+ 2
Ok, just FYI, all() or any() needs an iterable object to work with. I don't understand why would you need either one of those functions to check for even or odd number.
20th Sep 2020, 1:36 PM
Ipang
+ 2
Try this I think this is what you want. print("NO" if any([n % 2 for n in b]) else "YES") An example is also available in the Python tutorial https://www.sololearn.com/learn/Python/2456/
20th Sep 2020, 1:48 PM
Ipang
+ 2
Ipang thanks!
20th Sep 2020, 1:49 PM
Petr
+ 1
Ipang Ok. It was a code where are you input 10 numbers:)))) and after i look all numbers aret even or not
20th Sep 2020, 1:32 PM
Petr
+ 1
Ipang i want without "for" Maybe print("yes" if any(b[i]%2) else "no")
20th Sep 2020, 1:40 PM
Petr
0
No sweat buddy 👌
20th Sep 2020, 1:50 PM
Ipang