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

I have two numpy arrays

14
of not equal length. How to find the elements that are both in array1 and array2? Thanks!!!
Dec 3 '09 #1
1 1551
Glenton
391 Expert 256MB
Sounds like a job for sets.
See docs here

Expand|Select|Wrap|Line Numbers
  1. In [21]: a
  2. Out[21]: array([1, 2, 3, 4])
  3.  
  4. In [22]: b
  5. Out[22]: array([2, 4, 4, 5, 5])
  6.  
  7. In [23]: a2=set(a)
  8.  
  9. In [24]: b2=set(b)
  10.  
  11. In [25]: a2
  12. Out[25]: set([1, 2, 3, 4])
  13.  
  14. In [26]: b2
  15. Out[26]: set([2, 4, 5])
  16.  
  17. In [27]: result = a2 & b2 #or a2.intersection(b2)
  18.  
  19. In [28]: result
  20. Out[28]: set([2, 4])
  21.  
Or you could loop through one of the arrays and check whether it's in the other.
Expand|Select|Wrap|Line Numbers
  1. In [29]: for i in a:
  2.    ....:     if i in b:
  3.    ....:         print i,
  4.    ....:         
  5.    ....:         
  6. 2 4
  7.  
  8.  
Dec 3 '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...
4
by: sonjaa | last post by:
Hi last week I posted a problem with running out of memory when changing values in NumPy arrays. Since then I have tried many different approaches and work-arounds but to no avail. I was...
1
by: Rolf Wester | last post by:
Hi, I want to concatenate two numpy arrays with shape (n1,n2) and (n1,n3) into a single array with shape (n1,n2+n3). I guess there is an elegant way to do this but I couldn't figure it out. So...
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
0
by: Robert Kern | last post by:
You will want to ask numpy questions on the numpy mailing list: http://www.scipy.org/Mailing_Lists I need a little more time to mull on your problem to give you an actual answer, but I hope I...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
2
by: Travis Oliphant | last post by:
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one...
1
by: Slaunger | last post by:
Hi, This is my first post here, I am looking forward to being here. I have actually posted almost the same question on comp.lang.python: ...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.