473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Nested loop

I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?
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])

Nov 30 '05 #1
5 1447
viewcharts wrote:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?
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])


When the inner loop is finished for the 1st time,
myfile has been read. So next time you start to the
loop, myfile.readlines() returns an empty list.

Something like this should be better:

lookupSymList = myfile.readlines()
for refSymbol in symbols.readlines():
for lookupSymbol in lookupSymList:
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip()+" "+showme[10])
Nov 30 '05 #2

viewcharts wrote:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?
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])

As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.

Nov 30 '05 #3
bo****@gmail.com wrote:
viewcharts wrote:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?
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])
As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

The result of the readlines() function *is* a list, which is precisely
why it's been used up:
f = file("mail.py")
type(f.readlines()) <type 'list'>


A second call to readlines just gets an empty list, since there are no
more lines left to be read.
Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.

The solution, as already proposed, is to bind the list of lines to a
nanme so it can be reused.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 30 '05 #4
Steve Holden wrote:
bo****@gmail.com wrote:
viewcharts wrote:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?

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])
As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

.... The solution, as already proposed, is to bind the list of lines to a
nanme so it can be reused.

regards
Steve


Or you could read each on the fly, and rewind the inner:

for refSymbol in symbols:
for lookupSymbol in myfile:
showme = lookupSymbol.split('\t')
if showme[3] == refSymbol.strip():
priceNew.write(refSymbol.strip() + " " + showme[10])
myfile.seek(0)

This is probably more what you wanted, but Steve's suggestion will run
much faster.

--Scott David Daniels
sc***********@acm.org
Nov 30 '05 #5
On 30 Nov 2005 00:37:43 -0800, bo****@gmail.com wrote:

viewcharts wrote:
I am reading two text files comparing the values in one to the other,
this requires two loops. The problem is that when the inner loop is
finished, it never goes back into the loop. Any suggestions?
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])

As another poster said, you have "used up" the inner iterable in the
first round, it is an iterable, just not like a list where you can use
multiple times.

Either turn it into a list(so you can reuse it) or better yet, turn it
into a dict which would speed up the matching process. Either way, it
better be done outside of the outer loop.

Yes, and unless there is an ordering requirement that can't be ignored or achieved
by sorting afterwards, symbols seems like it could be a set. E.g., (untested)

refSymbolSet = set(refSymbol.strip() for refSymbol in symbols)
for showme in (lookupSymbol.split('\t') for lookupSymbol in myfile):
if showme[3] in refSymbolSet:
priceNew.write(showme[3]+" "+showme[10])

It would probably be more robust to check for blank lines and showme missing fields
and symbol duplicates also.

Regards,
Bengt Richter
Nov 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
2
by: mark | last post by:
(not sure if this is the correct group) My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.