[Python.NET] Nested Loops

Thane thane at magna-capital.com
Wed Nov 30 23:51:00 CET 2005


Read in the first file and create a dictionary (hash) of each line.
Read in the second file and for each line see if the dictionary contains the
item.  This solution minimizes your I/O.

Python 2.4b1 (#57, Oct 15 2004, 15:23:38) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> refsymbol = [1,2,3,4]
>>> refdict = dict()
>>> for sym  in refsymbol:
... 	refdict[sym] = sym
... 
>>> refdict
{'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'}
>>> showme = ['d','e','f']
>>> for s in showme:
... 	if refdict.has_key(s):
... 		print s
... 
d
>>>

> -----Original Message-----
> From: pythondotnet-bounces at python.org [mailto:pythondotnet-
> bounces at python.org] On Behalf Of W G
> Sent: Tuesday, November 29, 2005 2:53 PM
> To: pythondotnet at python.org
> Subject: [Python.NET] Nested Loops
> 
> Hello,
> 
> The follow code willl read lines of two text files. It supposed to take
> the
> first line of the first text file and compare it to all the lines of the
> second text file, then go to the next line of the first text file and do
> the
> same and so on.
> 
> The problem is that once the inner loop is finished, it never goes in that
> loop again. Any suggestions?
> 
> Thank you,
> Wes
> 
> 
> The Code:
> 
> 
> for refSymbol in symbols.readlines():
>     for lookupSymbol in myfile.readlines():
>         showme = lookupSymbol.split('\t')
>         if showme[3] == refSymbol.strip():
>             priceNew.write(refSymbol.strip()+" "+showme[10])
> 
> 
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet



More information about the PythonDotNet mailing list