Re: [Tutor] nested loops
Alan Gauld wrote:- Wayne Werner waynejwer...@gmail.com 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
Alan Gauld wrote: Wayne Werner waynejwer...@gmail.com 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
Steven D'Aprano st...@pearwood.info 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
[Tutor] nested loops
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 ___ Tutor maillist - Tutor@python.org 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