[Tutor] For - if - else loop; print selective output
Asokan Pichai
pasokan at talentsprint.com
Thu Oct 25 12:15:48 CEST 2012
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
> not found
>
> I want it to print "yes" for each positive match but nothing for a negative
> match. However if all matches are negative, I want it to print "Not found"
> once (That bit the code already does). I do I get it to print "yes" only in
> a mix result situation?
>
> Saad
I am sure you have some answer(s) already. I wanted to add a different solution
------------------------
a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']]
matches = [ i[1] in ('25', '26') for i in a ] ]
if not any(matches):
print "Not found"
else:
for match in matches:
if match:
print "Yes"
------------------------
Asokan Pichai
--
More information about the Tutor
mailing list