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
#1
i have a big (nearly a million lines) file that is being read in, one line at a time.  each line is .split() and there are about 3 dozen tokens for each line.  several tokens are checked to select which lines are to be used.  the check is an equality check for a few different tokens.  the tokens being checked are not contiguous, such as 4, 5, 9, 10, and 12 are checked.  i am doing these checks with a big long if statement with lots of ands.  i am wondering if there is any sweeter way to code this kind of thing. a program i am working on today is doing a lot of this kind of thing, from lots of cloud data i have.

    datadict = {}
    for line in sys.stdin:
       tokens = line.rstrip().split()
       if tokens[4] == 'foo' and\
          tokens[5] == 'bar' and\
          tokens[9] == 'xyzzy' and\
          tokens[10] == 'yzzyx' and\
          tokens[12] == 'Skaperen':
           processed += 1
           datadict[tokens[0]] = (tokens[1],tokens[2],tokens[3])
       elif tokens[4] == 'bar' and\
            tokens[5] == 'foo' and\
            tokens[9] == 'yzzyx' and\
            tokens[11] == 'xyzzy' and\
            tokens[12] == 'Skapare':
           processed += 1
           datadict[tokens[0]] = (tokens[2],tokens[1],tokens[3])
       else:
          skipped += 1
comparing long slices is not practical since the data to be checked is on either side of data that can vary.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
looking for sweeter code to compare parts of a list - by Skaperen - Jun-18-2017, 05:21 AM

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