difference between 2 arrays

P

Pierre

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Thanks !
 
D

Diez B. Roggisch

Pierre said:
Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Not sure if this works with numpy-arrays, but

set(a) - set(b)

might just work if the arrays are iterable.

Diez
 
M

Michel Claveau - MVP

Re !

Juste pour signaler qu'il existe un newsgroup en français sur Python, qui permet de recevoir des réponses en français (donc plus complètes/détaillées).

@-salutations
 
J

John Machin

See the module "sets"

See especially the notice at the front of the current sets doc which
says "deprecated since 2.6" and the comparison down the end which
explains why the built-in set() and frozenset() are better than the
sets module. Starting now to use the sets module is not a good idea
unless the OP is stuck on using Python 2.3 .
 
B

baalu aanand

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Thanks !



Hi,

Here I have given my logic, check whether it helps for you

a = [1,2, 3,2,5,2]
b = [1,2]
j = 0
dif = []
for i in range(len(a)) :
if a not in b:
dif.append(a)
j += 1

print dif[k]



Thanks
Baalu
 
B

baalu aanand

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Thanks !

Hi,

Here I have given my logic, check whether it helps for you

a = [1,2, 3,2,5,2]
b = [1,2]
j = 0
dif = []
for i in range(len(a)) :
if a not in b:
dif.append(a)
j += 1

print dif

Thanks
Baalu
 
D

David Robinow

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Thanks !

import numpy
a = numpy.array([1,2,3,2,5,2])
b = numpy.array([1,2])
c = list(set(a)-set(b))
# or c = numpy.array(list(set(a)-set(b))) if you want to continue w/ arrays
print c
 
D

Diez B. Roggisch

baalu said:
Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

I thought a.difference(b) could work, but no : AttributeError:
'numpy.ndarray' object has no attribute 'difference'

Thanks !



Hi,

Here I have given my logic, check whether it helps for you

a = [1,2, 3,2,5,2]
b = [1,2]
j = 0
dif = []
for i in range(len(a)) :
if a not in b:
dif.append(a)
j += 1

print dif[k]



EEEK, quadratic behavior!!

Diez
 
R

Robert Kern

Hello,

I would like to know how to find the difference (set operation)
between 2 arrays :

a = array([1,2, 3,2,5,2])
b = array([1,2])
I want a - b = [3,5]

Well, the equivalence of setdiff in matlab...

You will want to ask numpy questions on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

Using set() is frequently a good option, but for large arrays, you will want to
avoid the overhead of converting to and from sets and use numpy.setdiff1d(a, b):

In [2]: a = array([1,2, 3,2,5,2])

In [3]: b = array([1,2])

In [4]: numpy.setdiff1d(a, b)
Out[4]: array([3, 5])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
J

Jan Kaliszewski

19-08-2009 o 10:56:20 <""Michel Claveau -
MVP said:
(envoyé via
Hi!

See the module "sets"

No, see the builtin set type. Module sets is deprecated (removed in Py 3.x)
 
M

Michel Claveau - MVP

(envoyé via

Hi!

Yes, the module sets is written, in doc, like "deprecated".
But:
- sets exist in Python 2.6 (& 2.5 or 2.4)
- documentation of sets (module) is better tha, documentation of set (builtin)

The best: read the documentaion of the module, and use the builtin...

@-salutations
 
G

Gabriel Genellina

En Thu, 20 Aug 2009 06:54:05 -0300, <""Michel Claveau -
MVP said:
Yes, the module sets is written, in doc, like "deprecated".
But:
- sets exist in Python 2.6 (& 2.5 or 2.4)
- documentation of sets (module) is better tha, documentation of set
(builtin)

The best: read the documentaion of the module, and use the builtin...

Any suggestion to improve the builtin set documentation? In what ways do
you see one is better than the other?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top