Reputation: 14002
I have a list like this
L=['d','f','d','c','c','f','d','f']
and i would like to count how many d,f and c occurences are in L and store the result like:
R=[['d',3],['f',3],['c',2]]
What is the best approch (algorithm) ?
Upvotes: 2
Views: 147
Reputation: 63737
A possible solution using itertools.groupby on a sorted data
Implementation
from itertools import groupby
[[k, len(list(v))] for k, v in groupby(sorted(L))]
Output
[['c', 2], ['d', 3], ['f', 3]]
Performance Comparison
In [9]: L = [choice(ascii_letters) for _ in range(1000)]
In [10]: %timeit [[k, len(list(v))] for k, v in groupby(sorted(L))]
1000 loops, best of 3: 271 us per loop
In [11]: %timeit Counter(L).items()
1000 loops, best of 3: 306 us per loop
Note
It should be noted that the overhead in the Counter Solution in hashing the data, overshoots the Sorting Complexity in Tim's Sort
Upvotes: 1
Reputation: 239473
L=['d','f','d','c','c','f','d','f']
from collections import Counter
print Counter(L)
Output
Counter({'d': 3, 'f': 3, 'c': 2})
You can use Counter.most_common
method to get the result like this
print Counter(L).most_common()
Output
[('d', 3), ('f', 3), ('c', 2)]
Upvotes: 1
Reputation: 82470
I feel a dictionary would be better for this:
>>> from collections import Counter
>>> L = ['d','f','d','c','c','f','d','f']
>>> Counter(L)
Counter({'d': 3, 'f': 3, 'c': 2})
However, if you're adamant about a list of lists:
>>> L=['d','f','d','c','c','f','d','f']
>>> from collections import Counter
>>> var = Counter(L)
>>> [[key, value] for key, value in var.items()]
[['c', 2], ['d', 3], ['f', 3]]
Upvotes: 2
Reputation: 23231
The best approach (algorithm), is to not do it yourself!
>>> from collections import Counter
>>> L=['d','f','d','c','c','f','d','f']
>>> Counter(L)
Counter({'d': 3, 'f': 3, 'c': 2})
If you insist on a list:
>>> Counter(L).items()
[('c', 2), ('d', 3), ('f', 3)]
Upvotes: 5