473,385 Members | 1,752 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,385 software developers and data experts.

numpy and python array index question

14
Dear all:

I have this function and it works just fine but it is slow:

Expand|Select|Wrap|Line Numbers
  1. def common_index(list1, list2, val1, val2):
  2.    assert len(list1) == len(list2), "List not of equal length"
  3.    for i in range(len(list1)):
  4.       if list1[i] == val1 and list2[i] == val2:
  5.          return [i] 
  6. To optimize it:
  7. coord_ind=numpy.where(list1==val1 and list2==val2) 
Yet this line does not work and always returns ()? Any suggestions? Thanks!!!
Dec 1 '09 #1
1 2928
Glenton
391 Expert 256MB
As with my other post you probably want to be working in numpy. And using what I believe are called masks is a great idea.

We'll change list1 and list2 to array1 and array2. We're looking for a list of the indexes where array1[i]==val1 and array2[i]==val2.

Expand|Select|Wrap|Line Numbers
  1. indices=arange(n)   #this creates an array of the indices you're after
  2. mask1=array1==val1  #this creates an array like [True, False, False, True,...]
  3. mask2=array2[mask1]==val2  #(no need to check all of the value!)
  4.                                    #array2[mask1] is a smaller array
  5. return indices[mask1][mask2]
  6.  
I haven't tried it yet, but it *should* work, and it *should* be a ton quicker...
[/code]
Dec 2 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: 2mc | last post by:
I am finding out all kinds of ways to do things in NumPy through the many suggestions I have received. It's exciting. Thanks to all who have replied on my other threads. I'm still having...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
20
by: mclaugb | last post by:
Has anyone recompiled the Scientific Computing package using NumPy instead of Numeric? I need a least squares algorithm and a Newton Rhaphson algorithm which is contained in Numeric but all the...
2
by: robert | last post by:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0 Why is...
2
by: mcdurr | last post by:
I recently installed Python 2.5 on Windows and also installed numpy 1.0. I'd like to compute an FFT on an array of numbers but I can't seem to access the FFT function. I'm fairly new to Python...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
1
by: Jianzhong Liu | last post by:
Hello, Guys, I have a question about the linear_least_squares in Numpy. My linear_least_squares cannot give me the results. I use Numpy1.0. The newest version. So I checked online and get...
0
by: Marc Oldenhof | last post by:
Hello, I'm trying to use a numpy array in C++ (win2000) using boost.python. Test code: void test( numeric::array& nsP) { object shape = nsP.getshape(); int rows = extract<int>(shape); int...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.