Kent Johnson wrote:

>Olli Rajala wrote:
>  
>
>>Hi again,
>>
>>I have a '2D array', I mean a list inside a list ([[][],[][],...])
and
>>would need to check if the value exists in it. Of course I could do
a
>>for loop, but that just seem to be a little overkill, so is there
any
>>other way to do it? I have to check the first cell of every insider
>>list.
>>    
>>
>
>With Python 2.4:
> >>> l= [ [1], [2,3], [4,5], [6]]
> >>> 2 in (i[0] for i in l)
>True
> >>> 3 in (i[0] for i in l)
>False
>
>with Python 2.3 use [i[0] for i in l] instead which is a little less
efficient.
>
The second case you show should be true because I believe that's what
he
asked for.  Here is I believe the correct way to do this.

>>> l= [ [1], [2,3], [4,5], [6]]
>>> def isin(test,alist):
...     return bool([x for x in alist if test in x])
...
>>> isin(2,l)
True
>>> isin(3,l)
True
>>> isin(10,l)
False
>>> def isin2(test,alist):
...     for x in alist:
...         if test in x:
...             return True
...     return False
>>> isin2(2,l)
True
>>> isin2(10,l)
False
>>>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to