473,396 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

where function

Is there a function in Python analogous to the "where" function in
IDL?

x=[0,1,2,3,4,2,8,9]
print where(x=2)

output:
[2,5]

Mar 18 '07 #1
6 1755
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

Mar 18 '07 #2
On 18 Mar, 15:19, vorticitywo...@gmail.com wrote:
Is there a function in Python analogous to the "where" function in
IDL?

x=[0,1,2,3,4,2,8,9]
print where(x=2)

output:
[2,5]
You can try this:

print filter( lambda x: a[x]==2, range(len(a)))

However it's not the best solution...

Mar 18 '07 #3
On Mar 18, 10:48 pm, bearophileH...@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!

Mar 18 '07 #4
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)



vo************@gmail.com wrote:
On Mar 18, 10:48 pm, bearophileH...@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
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 18 '07 #5
vo************@gmail.com wrote:
Is there a function in Python analogous to the "where" function in
IDL?

x=[0,1,2,3,4,2,8,9]
print where(x=2)

output:
[2,5]
If you're doing a lot of this kind of thing, you probably want to use
numpy::
>>import numpy
x = numpy.array([0, 1, 2, 3, 4, 2, 8, 9])
numpy.where(x == 2)
(array([2, 5]),)

You can find numpy here:

http://numpy.scipy.org/

STeVe
Mar 18 '07 #6
vo************@gmail.com wrote:
On Mar 18, 10:48 pm, bearophileH...@lycos.com wrote:
>[...fine solutions to the problem as asked...]
Thank you both, a little more cumbersome than I expected, but it does
the job! Thanks!
The obvious simple near-equivalent is:

data = range(33,99)
print data.index(45)

And "generalized":
data = [x % 9 for x in range(30)]
result = []
former = -1
try:
while True:
former = data.index(3, former + 1)
result.append(former)
except ValueError:
print result
else:
print 'Nothing found'

--
--Scott David Daniels
sc***********@acm.org
Mar 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
16
by: Dixie | last post by:
I have a problem using Dev Ashish's excellent module to concatenate the results of a field from several records into one record. I am using the code to concatenate certain awards onto a...
23
by: Steven D'Aprano | last post by:
I defined a nested function: def foo(): def bar(): return "bar" return "foo " + bar() which works. Knowing how Python loves namespaces, I thought I could do this:
5
by: Christian | last post by:
HI, I have a function that is used to constrain a query: Select COl1, Col2 From MyTable WHERE col1 = ... AND col2 = ... And MyFunction(col1) = ... My problem is that MyFunction is executed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.