A
A
AkaMaloy2021-08-02 15:12:36
Python
AkaMaloy, 2021-08-02 15:12:36

How to count the number of occurrences of elements in a list?

There is a list like:

[
  ['Егор', '1', 'Вася', '0'],
  ['Петя', '4', 'Вася', '5'],
  ['Егор', '2', 'Петя', '2']
]

How to calculate how many times Egor, Vasya, Petya are included in this list?
In this case, the answer should be Yegor: 2 Vasya: 2 Petya: 2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-08-02
@AkaMaloy

count = {}

for n in sum([ m[::2] for m in arr ], []):
  count[n] = count.get(n, 0) + 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question