Re: [Tutor] nested loops
"Steven D'Aprano" wrote I don't think you want to break out of *any* of the loops. Otherwise you will skip testing combinations. In that case I misread the OP. I thought he specifically wanted to avoid testing all the options and exit when he reached his target. In your example, the first time you set highscore and alignment, you break out of all three loops Yep, thats what I thought was being asked for. It seems I mistook the intent. reading too quickly I suspect. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
Alan Gauld wrote: "Wayne Werner" wrote You probably want to add a sentinel to break out of the outer loops too: I don't think you want to break out of *any* of the loops. Otherwise you will skip testing combinations. In your example, the first time you set highscore and alignment, you break out of all three loops and only test the first triple x,y,z. > found = False highscore = 0 alignment = somealignment for x in something and not found: for y in somethingelse and not found: for z in evenmoresomething: if x+y+z > highscore: highscore = x+y+z alignment = newalignment found = True break That's the equivalent of a return in the innermost loop. If you're looking for the *first* matching highscore, that's fine, but not if you want the biggest. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
Alan Gauld wrote:- > "Wayne Werner" wrote > found = False > > highscore = 0 > > alignment = somealignment > > for x in something and not found: > > for y in somethingelse and not found: > > for z in evenmoresomething: > > if x+y+z > highscore: > > highscore = x+y+z > > alignment = newalignment > found = True > break > > HTH, That's going to exit first time through, isn't it? -- Michael This mail was sent via Mail-SeCure System. This footnote confirms that this email message has been scanned by PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
"Wayne Werner" wrote You probably want to add a sentinel to break out of the outer loops too: found = False highscore = 0 alignment = somealignment for x in something and not found: for y in somethingelse and not found: for z in evenmoresomething: if x+y+z > highscore: highscore = x+y+z alignment = newalignment found = True break HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
On 02/07/2011 10:33 AM, Ashley F wrote: I am trying to write a function...(it's kind of like a long way to do BLAST but only comparing 2 sequences) I have 3 loops nested...Within those loops, I obtain a "best fit score" so to speak. I can get the program to run...but the loops run all the way to the end. I can't figure out how to print the best score found...and the alignment that went with it. (therefore, my results will only be correct if my last alignment is the highest scoring one--which it usually isn't) To try to clear this up... The part of my pseudocode that I'm having trouble putting into actual code in python is: "if that alignment has the best score seen so far save the score and that alignment" It would be much easier for people to help you if you would include the actual code you have written. We have no way to tell you are running to the end or not; we certainly have nothing showing 'a best fit' algorithm. If we can see the actual code we can at least show you where you might want to look for current and/or potential problems. Also, what OS are you using? What version of Python are you using? Robert Berman ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
On Mon, 7 Feb 2011, Ashley F wrote: To try to clear this up... The part of my pseudocode that I'm having trouble putting into actual code in python is: "if that alignment has the best score seen so far save the score and that alignment" Tip: It's helpful to send code, like perhaps the triple loop, that illustrates your problem. highscore = 0 alignment = somealignment for x in something: for y in somethingelse: for z in evenmoresomething: if x+y+z > highscore: highscore = x+y+z alignment = newalignment Is that something to the effect of what you're looking for? HTH, Wayne___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
On Mon, 22 Aug 2005, Kent Johnson wrote: > > Is there any way more efficient for run a nested loop? > > > > -- > > for a in list_a: > > for b in list_b: > > if a == b: break Hi Jonas, Depends on what we're trying to do. Is it necessary to have a nested loop here? What kind of problem is this trying to solve? If the question is: "are any elements in list_a shared in list_b?", then yes, we can avoid nested loops altogether. If we're concerned about efficiency, we can take advanatage of dictionaries, or use something like the 'set' data structure. http://www.python.org/doc/lib/module-sets.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
> Is there any way more efficient for run a nested loop? > > -- > for a in list_a: if a in list_b: break Should help a bit, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested loops
Jonas Melian wrote: > Is there any way more efficient for run a nested loop? > > -- > for a in list_a: > for b in list_b: > if a == b: break efficient in running time? lines of code? What you have is pretty simple, what don't you like about it? In Python 2.4 you could use a generator expression: for (a for a in list_a for b in list_b if a==b): break If you want to know which is faster you have to time them... Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor