whatever
whatever

Reputation: 1227

Loop to Match Parts of List

My code:

#prints out samenodes
f = open('newerfile.txt')
mylist = list(f)
count = 0
i = 1
while count < 1000:
    if mylist[i] == mylist[i+12] and mylist [i+3] == mylist [i+14]: 
        print mylist[i]
    count = count+1
        i = i+12

My intention is to look at elt 1, elt 2. If elt 1 == elt 13 AND elt 2==elt 14 I want to print elt 1. Then, I want to look at elt 13 and elt 14. If elt 2 matches elt 13+12 AND elt 14 matches elt 14+12 I want to print it. ETC...

There are certainly parts of my list that fit this criteria, but the program returns no output.

Upvotes: 1

Views: 123

Answers (2)

mrKelley
mrKelley

Reputation: 3524

One problem is your indices. Be advised that lists begin with an index of 0.

I'm surprised nobody's answered this yet:

#prints out samenodes
f = open('newerfile.txt')
mylist = list(f)
count = 0
i = 0
while count < 1000:
    #print mylist[i]
    #print mylist[i+12]
    #print mylist[i+13]
    #print mylist[i+14]
    #...use prints to help you debug
    if mylist[i] == mylist[i+12] and mylist [i+1] == mylist [i+13]: 
        print mylist[i]
    count = count+1
    i = i+12

This is probably what you want.

Upvotes: 3

RoadieRich
RoadieRich

Reputation: 6556

To iterate over multiple lists (technically, iterables) in "lockstep", you can use zip. In this case, you want to iterate over four versions of mylist, offset by 0, 12, 2 and 13.

zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:])

Next, you want the 0th, 12th, 24th, etc elements. This is done with slice:

slicedList = zippedLists[::12]

Then you can iterate over that:

for elt1, elt13, elt2, elt14 in slicedList:
    if elt1 == elt13 and elt2 == elt14:
        print elt1

Putting it together with the file operations, we get

#prints out samenodes
f = open('newerfile.txt')
mylist = list(f)

zippedLists = zip(mylist, mylist[12:], mylist[2:], mylist[13:])
slicedList = zippedLists[::12]

for elt1, elt13, elt2, elt14 in slicedList:
    if elt1 == elt13 and elt2 == elt14:
        print elt1

Code like this is generally considered more "pythonic" than your current version, as using list indexes are generally discouraged when you are iterating over the list.

Note that if you've got a huge number of elements in your list the above code creates (and destroys at some point) five extra lists. Therefore, you may get better memory performance if you use the equivalent functions in itertools, which uses lazy iterators to prevent copying lists needlessly:

from itertools import islice, izip

#prints out samenodes
f = open('newerfile.txt')
mylist = list(f)

zippedLists = itertools.izip(mylist, islice(mylist, 12), islice(mylist, 2), islice(mylist, 13))
slicedList = itertools.islice(zippedLists, 0, None, 12)

for elt1, elt13, elt2, elt14 in slicedList:
    if elt1 == elt13 and elt2 == elt14:
        print elt1

There's probably a way in itertools to avoid slurping the entire file into mylist, but I'm not sure I remember what it is - I think that is the use case for itertools.tee.

Upvotes: 2

Related Questions