Python Forum
Still do not get how Python iterates over a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still do not get how Python iterates over a file
#11
That is why I'm coming back to this forum, you guys sharing knowledge and teaching, people like me, how to write a better code and you do it without an attitude.

To supuflounder:
you are right some of you are over 50, I have to tell some of us are over 60 Tongue
So you just a kid to me Big Grin Big Grin Big Grin Big Grin

Thank you for coaching!
Thank you for the code!
Love you all! Wink
perfringo likes this post
Reply
#12
Here is simpler and more scalable version (simple is better than complex)
import io

file1 = io.StringIO("""\
ZPM911
ZPM1912
ZPM1919
ZPM4555
ZPM4556
ZPM4557
""")

list_2 = ['ZPM911,UP,DOWN','ZPM1912,UP,UP','ZPM1919,DOWN,DOWN']
set_2 = set(el.split(',', 1)[0] for el in list_2)

for line in file1:
    el = line.strip()
    if el in set_2:
        print(f'Matched ++++++ {el}')
    else:
        print(f'Not matched -- {el}')
tester_V likes this post
Reply
#13
Well, I hope someday I'll be able to do the same you do...

Thank you again!
I really appriciate it!
Reply
#14
is this what u want?
list_1 = ['ZPM911', 'ZPM1912', 'ZPM1919', 'ZPM4555', 'ZPM4556', 'ZPM4557']
list_2 = ['ZPM911,UP,DOWN', 'ZPM1912,UP,UP', 'ZPM1919,DOWN,DOWN']

for item in list_1:
    if item not in [i.split(",")[0] for i in list_2]:
        print(f" Not Matched -- {item}")
    else:
        print(f" Matched ++++++ {item}")
Output:
Matched ++++++ ZPM911 Matched ++++++ ZPM1912 Matched ++++++ ZPM1919 Not Matched -- ZPM4555 Not Matched -- ZPM4556 Not Matched -- ZPM4557
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Do not get how Python iterates over a file tester_V 12 1,702 Jan-29-2023, 01:49 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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