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
#5
maybe something like this

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
note I assume initial value  of processed, skipped i set before the snippet you provide. Not sure if you really need processed (you can always check the len of datadict). maybe if you want to compare processed + skipped to total number of records to process?
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  compare parts of a 2-tuple differently Skaperen 0 279 May-18-2024, 08:52 PM
Last Post: Skaperen
Question Missing parts of Code Felipe1991_GVT 3 453 Mar-22-2024, 05:58 PM
Last Post: deanhystad
  How to expand and collapse individual parts of the code in Atom Lora 2 1,282 Oct-06-2022, 07:32 AM
Last Post: Lora
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 14,968 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  Compare response and name list in experiment knoxvillerailgrind 3 2,323 Jul-26-2020, 12:23 PM
Last Post: deanhystad
  Having a hard time combining two parts of code. Coozeki 6 3,312 May-10-2020, 06:50 AM
Last Post: Coozeki
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 3,039 May-10-2020, 05:28 AM
Last Post: deanhystad
  how to compare a list to a list of lists kevthew 1 1,890 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Converting parts of a list to int for sorting menator01 2 2,342 Nov-03-2019, 03:00 PM
Last Post: menator01
  Adding adjacent parts of a list TrueStudentOfPython 1 2,444 Nov-09-2018, 02:40 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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