Re: [Tutor] For - if - else loop; print selective output

2012-10-26 Thread Alexandre BM
You can break the loop when you get a positive matches, and add a variable where you can count the number of positive matches, if it's equal to zero print 'not found' On 24/10/2012 17:43, Saad Javed wrote: Let me modify this example: a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies']

Re: [Tutor] For - if - else loop; print selective output

2012-10-26 Thread Alan Gauld
On 26/10/12 20:38, Mark Lawrence wrote: On 26/10/2012 17:58, eryksun wrote: Sorry, Saad; this is now *way* off topic... Apart from blatant trolling nothing is off topic on any Python mailing list. That's simply not true. The mailing lists mostly have clear topic areas. This one is for learn

Re: [Tutor] For - if - else loop; print selective output

2012-10-26 Thread Mark Lawrence
On 26/10/2012 17:58, eryksun wrote: Sorry, Saad; this is now *way* off topic... Apart from blatant trolling nothing is off topic on any Python mailing list. That is one of the joys of reading them. Contrast that to the mailing lists associated with the group of languages that are based on

Re: [Tutor] For - if - else loop; print selective output

2012-10-26 Thread eryksun
Sorry, Saad; this is now *way* off topic... On Thu, Oct 25, 2012 at 5:39 PM, Oscar Benjamin wrote: > >> degrades if there are many collisions). On average, you can check if a >> set/dict contains an item in constant time, i.e. O(1). The amortized >> worst case is O(n). > > Why do you say "*amorti

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Oscar Benjamin
On 26 October 2012 00:08, Steven D'Aprano wrote: > On 26/10/12 07:16, eryksun wrote: >> >> On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit >> wrote: >>> >>> >>> Do you happen to know offhand if there is a difference between >>> `in` vs. `in` vs. `in`? >> >> >> The "in" comparison (__contains__ me

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Steven D'Aprano
On 26/10/12 07:16, eryksun wrote: On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit wrote: Do you happen to know offhand if there is a difference between `in` vs. `in` vs. `in`? The "in" comparison (__contains__ method) is equivalent for list and tuple. It has to search through the sequence it

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Prasad, Ramit
eryksun wrote: > On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit > wrote: > > > > Do you happen to know offhand if there is a difference between > > `in ` vs. `in ` vs. `in `? > > The "in" comparison (__contains__ method) is equivalent for list and > tuple. It has to search through the sequence it

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Oscar Benjamin
On 25 October 2012 21:16, eryksun wrote: > On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit > wrote: >> >> Do you happen to know offhand if there is a difference between >> `in ` vs. `in ` vs. `in `? > > The "in" comparison (__contains__ method) is equivalent for list and > tuple. It has to search

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread eryksun
On Thu, Oct 25, 2012 at 3:46 PM, Prasad, Ramit wrote: > > Do you happen to know offhand if there is a difference between > `in ` vs. `in ` vs. `in `? The "in" comparison (__contains__ method) is equivalent for list and tuple. It has to search through the sequence item by item, which makes it an O

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Prasad, Ramit
eryksun wrote: > On Wed, Oct 24, 2012 at 3:24 PM, Alan Gauld wrote: > > On 24/10/12 18:49, eryksun wrote: > > > >> Using "in ['25', '26']" also checks a compiled tuple constant: > >> > >> >>> compile("x in ['25', '26']", '', 'eval').co_consts > >> ('25', '26', ('25', '26')) > >> > >> 3.x

Re: [Tutor] For - if - else loop; print selective output

2012-10-25 Thread Asokan Pichai
On Wed, Oct 24, 2012 at 09:27:30PM +0500, Saad Javed wrote: > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: > if (i[1] == '25' or i[1] == '26'): > print 'yes' > else: > print 'Not found' > > This prints: > yes > no

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Sander Sweers
Alan Gauld schreef op wo 24-10-2012 om 17:49 [+0100]: > I confess I'm not keen on the else part of a for loop and never use > it, > I think it leads to more confusion than anything. It doesn't do what > most folks seem to expect, it should probably be called 'end' or > something similar rather t

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread ALAN GAULD
>>> Using "in ['25', '26']" also checks a compiled tuple constant: >>> >... >>> 3.x adds frozenset support: >>> >>>      >>> compile("x in {'25', '26'}", '', 'eval').co_consts >>>      ('25', '26', frozenset({'25', '26'})) >> >> >> I confess I don't know what that means! > >Sorry, I was showing th

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Steven D'Aprano
On 25/10/12 03:49, Alan Gauld wrote: I confess I'm not keen on the else part of a for loop and never use it, I think it leads to more confusion than anything. It doesn't do what most folks seem to expect, it should probably be called 'end' or something similar rather than 'else' IMHO. I am kee

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread eryksun
On Wed, Oct 24, 2012 at 3:24 PM, Alan Gauld wrote: > On 24/10/12 18:49, eryksun wrote: > >> Using "in ['25', '26']" also checks a compiled tuple constant: >> >> >>> compile("x in ['25', '26']", '', 'eval').co_consts >> ('25', '26', ('25', '26')) >> >> 3.x adds frozenset support: >> >>

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Alan Gauld
On 24/10/12 18:49, eryksun wrote: Using "in ['25', '26']" also checks a compiled tuple constant: >>> compile("x in ['25', '26']", '', 'eval').co_consts ('25', '26', ('25', '26')) 3.x adds frozenset support: >>> compile("x in {'25', '26'}", '', 'eval').co_consts ('25', '26'

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread eryksun
On Wed, Oct 24, 2012 at 1:30 PM, Joel Goldstick wrote: > >>> if (i[1] == '25' or i[1] == '26'): > > like this > if i[1] in ('25', '26'): Using "in ['25', '26']" also checks a compiled tuple constant: >>> compile("x in ['25', '26']", '', 'eval').co_consts ('25', '26', ('25',

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Joel Goldstick
On Wed, Oct 24, 2012 at 12:49 PM, Alan Gauld wrote: > On 24/10/12 17:27, Saad Javed wrote: > >> a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', >> '21', 'cookies']] > > >> for i in a: I would change this (which works but I think is simpler >> if (i[1] == '25' or i[1]

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Don Jennings
On Oct 24, 2012, at 12:27 PM, tutor-requ...@python.org wrote: > Hi, > > a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', > '21', 'cookies']] > for i in a: >if (i[1] == '25' or i[1] == '26'): >print 'yes' > else: >print 'Not found' Suggestion: use names whi

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Alan Gauld
On 24/10/12 17:59, Saad Javed wrote: Thanks! If not matched: < does it mean that "if the value of matched is not true, print Not found"? Yes exactly. matched starts off as False and will remain that way until a match is found at which point we set it to True. -- Alan G Author of the Le

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Saad Javed
Thanks! If not matched: < does it mean that "if the value of matched is not true, print Not found"? print "Not found" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/t

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Alan Gauld
On 24/10/12 17:27, Saad Javed wrote: a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] for i in a: if (i[1] == '25' or i[1] == '26'): print 'yes' else: print 'Not found' I want it to print "yes" for each positive match but nothing

Re: [Tutor] For - if - else loop; print selective output

2012-10-24 Thread Saad Javed
Let me modify this example: a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] for i in a: *b = i[0]* if (i[1] == '25' or i[1] == '26'): print *b* else: print 'Not found' This will output: *jimmy* *Not found* *Not found* * * How do

[Tutor] For - if - else loop; print selective output

2012-10-24 Thread Saad Javed
Hi, a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']] for i in a: if (i[1] == '25' or i[1] == '26'): print 'yes' else: print 'Not found' This prints: yes not found I want it to print "yes" for each positive match but nothing for a negative m