[Tutor] question on array Operation
Steven D'Aprano
steve at pearwood.info
Wed Oct 29 23:06:29 CET 2014
On Wed, Oct 29, 2014 at 05:08:28PM +0100, jarod_v6 at libero.it wrote:
> Dear All,
>
> I have a long matrix where I have the samples (1to n) and then I have (1to j )elements.
> I would like to count how many times each j element are present on each samples.
Jarod, looking at your email address, I am guessing that English is not
your native language. I'm afraid that I cannot understand what you are
trying to do. Please show a *simple* example.
I will make a *guess* that you have something like this:
matrix = [ # three samples of seven values each
[2, 4, 6, 8, 6, 9, 5],
[3, 5, 1, 7, 9, 8, 8],
[1, 2, 0, 6, 6, 2, 1],
]
and then you want to count how many each element [0, 1, 2, 3, 4, 5, 6,
7, 8, 9] appear in each sample:
from collections import Counter
for i, sample in enumerate(matrix, 1):
c = Counter(sample)
print("Sample %d" % i)
print(c)
which gives me:
Sample 1
Counter({6: 2, 2: 1, 4: 1, 5: 1, 8: 1, 9: 1})
Sample 2
Counter({8: 2, 1: 1, 3: 1, 5: 1, 7: 1, 9: 1})
Sample 3
Counter({1: 2, 2: 2, 6: 2, 0: 1})
Does that help?
If not, you have to explain what you need better.
--
Steven
More information about the Tutor
mailing list