-4

For example

MyList=[a,a,a,c,c,a,d,d,d,b]

Returns

[4,2,3,1]
MainStreet
  • 129
  • 1
  • 1
  • 6

5 Answers5

1
from collections import Counter
MyList=['a','a','a','c','c','a','d','d','d','b']
Counter(MyList).values() # Counts's the frequency of each element
    [4, 2, 1, 3]
Counter(MyList).keys()  # The corresponding elements
    ['a', 'c', 'b', 'd']
cph_sto
  • 7,189
  • 12
  • 42
  • 78
  • according to https://stackoverflow.com/questions/2600191/how-to-count-the-occurrences-of-a-list-item this is kinda slow because it checks the type of the elements every time. It'd be better another approach if you don't need type checking – magicleon94 Dec 09 '18 at 09:58
0

just do it with dictionary :

counter_dict = {}
for elem in MyList:
    if elem in counter_dict.keys():
         counter_dict[elem] += 1
    else :
         counter_dict[elem] = 1 

at the end you have a dictionary with key with key , which is the element in the list , and value which is the number of appearances.

ddor254
  • 1,570
  • 1
  • 12
  • 28
0

If you need only a list:

# Your list
l1 = [1,1,2,3,1,2]
# Set will give you each element from l1 without duplicates
l2 = set(l1)
# Let`s see how set look
#print(l2)
# Create new empty list
l3 = []
# Use for loop to find count of each element from set and store that in l3
for i in l2:
   k = l1.count(i)
   l3.append(k)
# Check how l3 now looks like
print(l3)

Return:

[3, 2, 1]
Dmitriy Kisil
  • 2,858
  • 2
  • 16
  • 35
0

First you should change your list elements to a string type:

my_list = ['a','a','a','c','c','a','d','d','d','b']

Because a, b, c or d without quote(') don't represent any type,so then you'll get error.

Try to use python dictionary, as follows:

result ={}
for each in my_list:
    result.setdefault(each, 0) 
    result[each] += 1
print result.values()

Then this would be the output:

[4,2,3,1]
Kian
  • 1,319
  • 1
  • 13
  • 23
-1

A first try would be doing this:

occurrencies = {n:a.count(n) for n in set(a)}

This returns a dictionary which has the element as key, and its occurrences as value. I use set to avoid counting elements more than once.

This is not a one-pass approach and it has quadratic complexity in time, hence this could be really slow.

Here's a way you could do to get the same result with one pass, linear complexity:

def count_occurrencies(inputArray):
    result = {}
    for element in inputArray:
        if element not in result:
            result[element] = 1
        else:
            result[element] += 1
    return result
magicleon94
  • 4,887
  • 2
  • 24
  • 53
  • 1
    *This is not a one-pass approach* — worse, this is actually quadratic. – Norrius Dec 09 '18 at 11:38
  • The one-liner is quadratic. The `count_occurrencies` is linear. Dictionary `result` access is like a hash table, hence access is O(1), and it cycles through the `inputArray` only once – magicleon94 Dec 09 '18 at 11:40
  • Oh sorry I misread your comment, I'm gonna edit to be more specific, thanks for pointing that out – magicleon94 Dec 09 '18 at 11:41