where function

Shane Geiger sgeiger at ncee.net
Mon Mar 19 00:12:20 CET 2007


a = 
[0,1,2,3,4,2,8,9]                                                                                                                                           

                                                                                                                                                                

# method 
1                                                                                                                                                      

print [i for i in xrange(len(a)) if 
a[i]==2]                                                                                                                    

                                                                                                                                                                

def 
where(a,val):                                                                                                                                               

    return [i for i in xrange(len(a)) if 
a[i]==val]                                                                                                             

                                                                                                                                                                

# method 
2                                                                                                                                                      

print 
where(a,2)                                                                                                                                                

                                                                                                                                                                





vorticitywolfe at gmail.com wrote:
> On Mar 18, 10:48 pm, bearophileH... at lycos.com wrote:
>   
>> vorticitywo:
>>
>>     
>>> Is there a function in Python analogous to the "where" function in
>>> IDL?
>>>       
>> Python gives very elastic syntax, you can simply do:
>>
>> data = [0,1,2,3,4,2,8,9]
>> print [pos for pos, el in enumerate(data) if el==2]
>>
>> Bye,
>> bearophile
>>     
>
> Thank you both, a little more cumbersome than I expected, but it does
> the job! Thanks!
>
>   

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070318/eeebd942/attachment.vcf>


More information about the Python-list mailing list