[Numpy-discussion] Multiplicity of an entry

Nadav Horesh nadavh at visionsense.com
Mon Oct 26 22:27:13 EDT 2009


In principle you could use:

np.equal(a,a).sum(0)

but, for unknown reason, np.equal operates only on "normal" arrays. maybe you can transform the array to arrays of numbers, for example by hash.

  Nadav


-----הודעה מקורית-----
מאת: numpy-discussion-bounces at scipy.org בשם josef.pktd at gmail.com
נשלח: ב 26-אוקטובר-09 20:26
אל: Discussion of Numerical Python
נושא: Re: [Numpy-discussion] Multiplicity of an entry
 
On Mon, Oct 26, 2009 at 2:12 PM, Christopher Barker
<Chris.Barker at noaa.gov> wrote:
> Alan G Isaac wrote:
>> On 10/26/2009 4:04 AM, Nils Wagner wrote:
>>> how can I obtain the multiplicity of an entry in a list
>>> a = ['abc','def','abc','ghij']
>>
>> That's a Python question, not a NumPy question.
>
> but we can make it a numpy question!
>
> In [15]: a = np.array(['abc','def','abc','ghij'])
>
>
> In [16]: a
> Out[16]:
> array(['abc', 'def', 'abc', 'ghij'],
>       dtype='|S4')
>
> In [17]: for item in set(a):
>     print item, (a == item).sum()

It's *very* slow, when there are a large number of items.
numpy creates the full boolean array for each item.

see also  http://projects.scipy.org/scipy/ticket/905

Josef


>
> abc 2
> ghij 1
> def 1
>
> I'll leave pro=filing to the OP.
>
> -Chris
>
>
>
> --
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion at scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 3776 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20091027/6215fc49/attachment.bin>


More information about the NumPy-Discussion mailing list