week one with Python. I would like to compare the value of two arrays where order matters and print out text. Here's what I have:
a =[2, 3, 4, 2, 6]
b =[1, 2, 3, 4, 5]
c =[0, 1, 2, 3, 4]
compare = map(lambda x, y: x > y, a, b)
print(list(compare))
and I get:
[True, True, True, False, True]
Rather than printing the list(compare) True and False I would like to print 'list a is below list b' if False. It only needs to print once if more than one False is present. I've tried many print statements without success.
if compare == False:
print ('list a is below list b')
nothing
if compare is False:
print ('list a is below list b')
crickets
I've tried many other ways.
Is there also a way to do a loop comparing if a>b, a>c and b>c then print('list b is below list c'). Order between lists matters. I could do the compare = map lambda 3 times but I was wondering if there's a way to loop it?
Thank you!
CodePudding user response:
If you want to check compare
has at least one value False
, just use the in
operator.
if False in compare:
do action
CodePudding user response:
You can compare 3 lists at the same time and use all()
. all()
function returns True
iff all conditions in the iterator provided to it are True
. -
a =[2, 3, 4, 2, 6]
b =[1, 2, 3, 4, 5]
c =[0, 1, 2, 3, 4]
compare = map(lambda x, y, z: x > y and y > z, a, b, c)
print(not any(compare))
outputs -
False
because the 3rd index is False
.
If I update the a
-
>>> a =[2, 3, 4, 5, 6]
>>> b =[1, 2, 3, 4, 5]
>>> c =[0, 1, 2, 3, 4]
>>> compare = map(lambda x, y, z: x > y and y > z, a, b, c)
>>> print(all(compare))
True