How to count how many times a pair of specific values ​​is repeated in an array?

I have two vectors of the same size: one for the wave height and one for the period corresponding to one at the same moment in time at which the measurement was taken. I want to know how many times two specific data is repeated, for example:

Hs = [0.5 1.0 2.3 0.5 0.5]

Tm = [2.0 2.5 2.0 2.0 3.0]

So you can see:

Hs Tm Count

0.5 2.0 2

0.5 2.5 0

0.5 3.0 1

1.0 2.0 0

1.0 2.5 1 ...

I tried, but the following errors occur because I see whole rows and columns without data and when I see my information for values.

from numpy import *
from matplotlib.pyplot import *
import matplotlib.pyplot as plt
from time import *

clf; cla; close
dat = loadtxt("ecmwf.dat", unpack=True)
HSf = dat[0,:]
HSf = around(HSf,decimals=1)
TMf = dat[1,:]
TMf = around(TMf,decimals=1)
mmat = zeros((31,141))

vhs = linspace(0.0,3.0,31)
vtm = linspace(0.0,14.0,141)

for i in xrange(0, vtm.size):
for k in xrange(0, vhs.size):
    if all((k <= vhs.size) & (i <= vtm.size)):
        lg1 = (TMf == vtm[i]) & (HSf == vhs[k])
        lg2 = sum(lg1)
    if lg2>=1:
        fg1 = text(i,k, str(lg2),horizontalalignment='center', verticalalignment='center',fontsize=6)
    mmat[k,i] = lg2
+4
source share
4 answers

Counter .

from collections import Counter

Hs = [0.5, 1.0, 2.3, 0.5, 0.5]
Tm = [2.0, 2.5, 2.0, 2.0, 3.0]

occurrences = Counter(zip(Hs, Tm))
for h in sorted(set(Hs)):
    for t in sorted(set(Tm)):
        print h, t, occurrences[(h,t)]

:

0.5 2.0 2
0.5 2.5 0
0.5 3.0 1
1.0 2.0 0
1.0 2.5 1
1.0 3.0 0
2.3 2.0 1
2.3 2.5 0
2.3 3.0 0
+1

Counter python 2.7:

import collections

Hs = [0.5, 1.0, 2.3, 0.5, 0.5]

Tm = [2.0, 2.5, 2.0, 2.0, 3.0]

pairs = zip(Hs, Tm)

, :

>>> print(list(pairs))
[(0.5, 2.0), (1.0, 2.5), (2.3, 2.0), (0.5, 2.0), (0.5, 3.0)]

So

pairs = zip(Hs, Tm)
counts = collections.Counter(pairs)

print(counts)

:

Counter({(0.5, 2.0): 2, (1.0, 2.5): 1, (0.5, 3.0): 1, (2.3, 2.0): 1})

Counter - dict, dict:

for pair, count in counts.items():
    print(pair, count)

:

(1.0, 2.5) 1
(0.5, 3.0) 1
(0.5, 2.0) 2
(2.3, 2.0) 1

, , , dict:

counts[(1.0, 3.0)]

0
+1

You can use collections.Counterfor this task

import collections
import itertools

hs = 0.5, 1.0, 2.3, 0.5, 0.5
tn = 2.0, 2.5, 2.0, 2.0, 3.0

pairCount = collections.Counter(itertools.izip(hs, tm))

print(pairCount)

You should get something like:

Counter({(0.5, 2.0): 2, (1.0, 2.5): 1, (2.6, 2.0): 1, (0.5, 3.0): 1})
0
source

collections.Counter () will count the number of passes in an iterable:

>>> import numpy as np 
>>> from collections import Counter
>>> Hs = [0.5, 1.0, 2.3, 0.5, 0.5]
>>> Tm = [2.0, 2.5, 2.0, 2.0, 3.0]
>>> repeats = Counter(zip(Hs,Tm))
>>> 
>>> aHs = np.array(Hs)
>>> aTm = np.array(Tm)
>>> aRepeats = Counter(zip(aHs,aTm))
>>> aRepeats
Counter({(0.5, 2.0): 2, (2.2999999999999998, 2.0): 1, (1.0, 2.5): 1, (0.5, 3.0): 1})
>>> 
>>> aRepeats[(0.5, 2.0)]
2
>>> repeats[(1.0, 2.5)]
1
>>>
0
source

Source: https://habr.com/ru/post/1527781/


All Articles