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


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