How to cleanly loop over two files in parallel in Python?

It looks like you don't care what the value of I is. You just are using it as a way to pair up the lines and the words . Therefore, I recommend you read one line at a time, and at the same time read one word.

Then they will match.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I frequently write code like: lines = open('wordprob. Txt','r').readlines() words = open('StdWord. Txt','r').readlines() I = 0 for line in lines: v = eval(s) for s in line.split() if v0 > v1: print wordsi.strip(), I += 1 Is it possible to avoid use variable I and make the program shorter?

Thanks. Python link|improve this question edited Apr 28 '11 at 20:20steveha7,254624 asked Dec 2 '09 at 3:53Yin Zhu8,21311040 85% accept rate.

It looks like you don't care what the value of I is. You just are using it as a way to pair up the lines and the words. Therefore, I recommend you read one line at a time, and at the same time read one word.

Then they will match. Also, when you use .readlines() you read all the input at once into memory. For large inputs, this will be slow.

For this simple code, one line at a time is all you need. The file object returned by open() can act as an iterator that returns one line at a time. If you can, you should avoid the use of eval().

In a simple exercise where you know what the input data will be, it is pretty safe, but if you get data from outside sources, the use of eval() could possibly allow your computer to be attacked. See this page for more info. I will write my example code to assume that you are using eval() to turn text into a float value.

Float() will work on an integer string value, too: float('3') will return 3.0. Also, it appears that the input lines can only have two values. If a line ever has extra values, your code will not detect this condition.

We can change the code to explicitly unpack two values from the split line, and then if there are more than two values, Python will raise an exception. Plus, the code will be slightly nicer to read. So here is my suggested rewrite of this example: lines = open('wordprob.

Txt','rt') words = open('StdWord. Txt','rt') for line in lines: word = words.next().strip() # in Python 3: word = next(words).strip() a, be = float(s) for s in line.split() if a > b: print word, # in Python 3: print(word + ' ', end='') EDIT: And here is the same solution, but using izip(). Import itertools lines = open('wordprob.

Txt','rt') words = open('StdWord. Txt','rt') # in Python 3, just use zip() instead of izip() for line, word in itertools. Izip(lines, words): word = word.strip() a, be = float(s) for s in line.split() if a > b: print word, # in Python 3: print(word + ' ', end='') In Python 3, the built-in zip() returns an iterator, so you can just use that and not need to import itertools.

EDIT: It is best practice to use a with statement to make sure the files are properly closed, no matter what. In recent versions of Python you can have multiple with statements, and I'll do that in my solution. Also, we can unpack a generator expression just as easily as we can unpack a list, so I've changed the line that sets a, be to use a generator expression; that should be slightly faster.

And we don't need to strip word unless we are going to use it. Put the changes together to get: from itertools import izip with open('wordprob. Txt','rt') as lines, open('StdWord.

Txt','rt') as words: # in Python 3, just use zip() instead of izip() for line, word in izip(lines, words): a, be = (float(s) for s in line.split()) if a > b: print word.strip(), # in Python 3: print(word.strip() + ' ', end='').

Thanks for you valuable comments! – Yin Zhu Dec 2 '09 at 8:48 You are very welcome! :-) – steveha Dec 2 '09 at 19:09.

You can try to use enumerate, docs.python.org/tutorial/datastructures.... lines = open('wordprob. Txt','r').readlines() words = open('StdWord. Txt','r').readlines() for i,line in enumerate(lines): v = eval(s) for s in line.split() if v0 > v1: print wordsi.strip().

In general enumerate is a good solution. In this case, you could do something like: lines = open('wordprob. Txt','r').readlines() words = open('StdWord.

Txt','r').readlines() for word, line in zip(words, lines): v = eval(s) for s in line.split() if v0 > v1: print word.strip().

3 zip() is a good way to loop with two things together. However, in Python 2. X, it will build a list with all values, so this will take a lot of memory.

You can get the same effect with itertools.izip(), which returns an iterator that returns the values one at a time. You could import itertools and then do this: for word, line in itertools. Izip(open('wordprob.

Txt'), open('StdWord. Txt')): – steveha Dec 2 '09 at 4:52.

Take a look at enumerate: >>> for i, season in enumerate('Spring', 'Summer', 'Fall', 'Winter'): ... print i, season 0 Spring 1 Summer 2 Fall 3 Winter.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions