Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"any" retunes an error
#1
Hi, I'm getting an error:
'bool' object is not iterabler" when running this snippet and I do not understand why.
import os
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for x in tof :
        for ln in th_th :
            if any(x in ln) :
                print (ln)
And no errors when running this one (see below):

import os
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for ln in th_th :
        if any(x in ln for x in tof):     
            print (ln)
They look the same to me but apparently, they are not Sad.

Could anyone elaborate?

Thank you!
Reply
#2
any() takes something that can be iterated over, that you can make multiple tests on.

x in y is a single expression. It returns one value and isn't appropriate for any()

>>> "x" in "this string"
False
>>> any("x" in "this string")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'bool' object is not iterable
In addition, x and ln are static at this point in the code, so any() doesn't seem useful here. You're changing the variables in a loop and checking one thing each time through the loop.

For your second snippet, x in ln for x in tof, this is a generator comprehension and you can iterate over it. Therefore any() will return true if any of the elements are true.

>>> list(x in "The quick" for x in "abcdef")
[False, False, True, False, True, False]
>>> any(x in "The quick" for x in "abcdef")
True
Reply
#3
To bowlofred,
Thank you for your reply!
it is still confusing.
Reply
#4
(Nov-17-2020, 08:53 PM)tester_V Wrote: They look the same to me but apparently, they are not Sad.

it is still confusing.

These snippets of code are anything but same.

First one reads:

for every item in list:
    for every line in file


This means that for every item in list you go through all lines of file. If file is long and list is short this is not very optimal algorithm.

Second one reads:

for every line in file:
    is there any item from list in line
This means that you go through file line by line and for every line check whether any item list is present. any() has short circuit feature i.e. it stops after first True.

I don't know whether it is a problem or not, but if line is 'computer network' then current code finds 'two' from that line.
tester_V likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
python comprehensions can be confusing at first if you're not used to them. They're a very compact way to express a collection of info.

You don't have to use them. You could just write it as a regular loop.

Explicit loop:
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    for line in th_th:
        for word in tof:
            if word in line:
                print(line)
                break
Comprehension:
tof = ['try','two']
with open('C:/02/file1.txt', 'r') as th_th :
    print([line for line in th_th if any(word in line for word in tof)])
tester_V likes this post
Reply
#6
Guys thank you for the explanations!
I'm new to Python and I'm not a programmer and yes, it is confusing for now...

I really appreciate your help!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020