0

I am using Python3. I have a list a of only integers. Now, I want to save the element and the number it repeats itself in a row in another list.

Example:

a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]

Output:

result = ["6,1", "0, 2", "2, 4", "1, 1", "89, 2"]    
# the number before the "," represents the element, the number after the "," represents how many times it repeats itself.

How to efficiently achieve my goal ?

codrelphi
  • 1,075
  • 1
  • 7
  • 13
Junji
  • 11
  • 5
  • Does this answer your question? [How can I count the occurrences of a list item?](https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item) – Aditya Shankar Nov 16 '19 at 19:20

6 Answers6

3

I believe all the solutions given are counting the total occurrences of a number in the list rather than counting the repeating runs of a number.

Here is a solution using groupby from itertools. It gathers the runs and appends them to a dictionary keyed by the number.

from itertools import groupby

a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
d = dict()

for k, v in groupby(a):
    d.setdefault(k, []).append(len(list(v)))

Dictionary created:

>>> d
{6: [1], 0: [2], 2: [4], 1: [1], 89: [2]}

Note that all runs only had 1 count in their list. If there where other occurrences of a number already seen, there would be multiple counts in the lists (that are the values for dictionary).

Chris Charley
  • 6,403
  • 2
  • 24
  • 26
1

for counting an individual element, us list.count,

i.e, here, for, say 2, we user a.count(2), which outputs 4,

also, set(a) gives the unique elements in a

overall answer,

a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
nums = set(a)
result = [f"{val}, {a.count(val)}" for val in set(a)]
print(result)

which gives

['0, 2', '1, 1', '2, 4', '6, 1', '89, 2']
Aditya Shankar
  • 702
  • 6
  • 12
1

Method 1: using for loop

a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
result = []
a_set = set(a) # transform the list into a set to have unique integer
for nbr in a_set:
    nbr_count = a.count(nbr)
    result.append("{},{}".format(nbr, nbr_count))

print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']

Method 2: using list-comprehensions

result = ["{},{}".format(item, a.count(item)) for item in set(a)]
print(result) # ['0,2', '1,1', '2,4', '6,1', '89,2']
codrelphi
  • 1,075
  • 1
  • 7
  • 13
0

you can use Python List count() Method, method returns the number of elements with the specified value.

a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]

print ({x:a.count(x) for x in a})

output:

{6: 1, 0: 2, 2: 4, 1: 1, 89: 2}
ncica
  • 7,015
  • 1
  • 15
  • 37
0
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
dic = dict()
for i in a:
     if(i in dic):
             dic[i] = dic[i] + 1
     else:
             dic[i] = 1

result = []
for i in dic:
     result.append(str(i) +"," + str(dic[i]))

Or:

from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
mylist = [Counter(a)]
print(mylist)
Duarte Castanho
  • 305
  • 2
  • 6
  • 20
0

You can use Counter from collections:

from collections import Counter
a = [6, 0, 0, 2, 2, 2, 2, 1, 89, 89]
counter = Counter(a)    
result = ['{},{}'.format(k, v) for k,v in counter.items()]
javad
  • 498
  • 4
  • 15