Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"any" retunes an error
#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


Messages In This Thread
"any" retunes an error - by tester_V - Nov-17-2020, 08:53 PM
RE: "any" retunes an error - by bowlofred - Nov-17-2020, 09:15 PM
RE: "any" retunes an error - by tester_V - Nov-17-2020, 10:25 PM
RE: "any" retunes an error - by perfringo - Nov-17-2020, 11:33 PM
RE: "any" retunes an error - by bowlofred - Nov-17-2020, 11:34 PM
RE: "any" retunes an error - by tester_V - Nov-18-2020, 12:18 AM

Forum Jump:

User Panel Messages

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