Python Forum
looking for sweeter code to compare parts of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looking for sweeter code to compare parts of a list
#7
(Jun-19-2017, 02:54 AM)Skaperen Wrote: that we the details of each type of test is away from the loop going through all the lines.
Not sure I understand that...
it exit the loop after first match, so not all test are performed.
I also was thinking that if you put the check in a separate function, it can make the code more clear and contained, although maybe a bit longer
By the way - I see I have a mistake in the above code - the else is part of the if statement, but it should be part of the for loop (as was my intention). In the above code the count of skipped is wrong.

datadict = {}
options = [({4:'foo', 5:'bar', 9:'xyzzy', 10:'yzzyx', 14:'Skaperen'}, (1, 2, 3)),
           ({4:'bar', 5:'foo', 9:'yzzyx', 11:'xyzzy', 14:'Skaperen'}, (2, 1, 3))]
for line in sys.stdin:
    tokens = line.rstrip().split()
    for opt, get_tokens in options:
        if all(tokens[k] == value for k, value in opt.items()):
            processed += 1
            datadict[tokens[0]] = tuple((tokens[i] for i in get_tokens))
            break
    else:
        skipped += 1
Reply


Messages In This Thread
RE: looking for sweeter code to compare parts of a list - by buran - Jun-19-2017, 07:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Missing parts of Code Felipe1991_GVT 3 353 Mar-22-2024, 05:58 PM
Last Post: deanhystad
  How to expand and collapse individual parts of the code in Atom Lora 2 1,211 Oct-06-2022, 07:32 AM
Last Post: Lora
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 14,764 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  Compare response and name list in experiment knoxvillerailgrind 3 2,262 Jul-26-2020, 12:23 PM
Last Post: deanhystad
  Having a hard time combining two parts of code. Coozeki 6 3,172 May-10-2020, 06:50 AM
Last Post: Coozeki
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,942 May-10-2020, 05:28 AM
Last Post: deanhystad
  how to compare a list to a list of lists kevthew 1 1,837 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Converting parts of a list to int for sorting menator01 2 2,267 Nov-03-2019, 03:00 PM
Last Post: menator01
  Adding adjacent parts of a list TrueStudentOfPython 1 2,392 Nov-09-2018, 02:40 AM
Last Post: ichabod801
  Compare element of list with line of file : if match, delete line silfer 4 3,557 Jul-21-2018, 02:44 PM
Last Post: silfer

Forum Jump:

User Panel Messages

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