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] 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
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...
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!
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 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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;
};
|
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...
|
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:
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |