Python syntax issue "any"

I have a problem with the syntax "any". I have two lists.

mainseq=["hali","hulu","habi"]
seq=["a","b","c","d"]

I want to find if elements exist in seq in mainseq.

for each in seq:
    if any(each in halum for halum in mainseq):
        print each

This gives me a and b, as expected. But when I delete β€œany” syntax, I get all the values ​​in seq, although β€œc” and β€œd” do not exist in mainseq.

for each in seq:
    if (each in halum for halum in mainseq):
        print each

What happens behind the scenes and WITHOUT "any" function? Please, help.

+4
source share
5 answers

To understand what is going on, you can debug it by printing out the equivalent list view booleanand view and any:

for each in seq:
    print(each)
    print([each in halum for halum in mainseq])
    print(any(each in halum for halum in mainseq))
    print(bool(each in halum for halum in mainseq)) 

# a
# [True, False, True] <- contains more than one True element
# True                <- so any returns True
# True                <- boolean expression of a generator is always True

# b
# [False, False, True] <- contains one True element
# True                 <- so any returns True
# True                 <- boolean expression of a generator is always True

# c
# [False, False, False] <- no element is True
# False                 <- so any returns False
# True                  <- boolean expression of a generator is always True

# d
# [False, False, False] <- no element is True
# False                 <- so any returns False
# True                  <- boolean expression of a generator is always True

, True, (. - ).

any, , True, True. each == a each == b. if .

+3

any True False.

(each in halum for halum in mainseq) , , if. , :

http://ideone.com/AQ7dRs

mainseq=["hali","hulu","habi"]
seq=["a","b","c","d"]

print('Demo of any:\n')
for each in seq:
    boolFlag = any(each in halum for halum in mainseq)
    print('Any returned:{0}'.format(boolFlag)) 
    if boolFlag:
        print each


print('Demo of generator as truthy value:\n')

for each in seq:
    boolFlag = (each in halum for halum in mainseq)
    print('boolFlag is:{0}'.format(boolFlag))
    if boolFlag:
        print each
        print('{0} is Truthy'.format(boolFlag))

:

Any returned:True
a
Any returned:True
b
Any returned:False
Any returned:False
Demo of generator as truthy value:

boolFlag is:<generator object <genexpr> at 0xb7272bbc>
a
<generator object <genexpr> at 0xb7272bbc> is Truthy
boolFlag is:<generator object <genexpr> at 0xb7272be4>
b
<generator object <genexpr> at 0xb7272be4> is Truthy
boolFlag is:<generator object <genexpr> at 0xb7272bbc>
c
<generator object <genexpr> at 0xb7272bbc> is Truthy
boolFlag is:<generator object <genexpr> at 0xb7272be4>
d
<generator object <genexpr> at 0xb7272be4> is Truthy
+2

Any true, 1 . , , :

for each in seq:
    for halum in mainseq:
        if each in halum:
            print each
+1

(each in halum for halum in mainseq) . . if True, . any() , . , - True.

+1

any() True, - . , False.

So, it if any(each in halum for halum in mainseq):scans the elements inside. iterations where each "a" or "b" contains these elements, and therefore if the statement is true. For the cases of "c" and "d", iterable is empty, creating false.

When you remove any (), you no longer look inside the iteration, but just ask if iterable is not true.

if (each in halum for halum in mainseq):

will always be true, since it will always generate a valid generator object.

+1
source

All Articles