tags:

views:

455

answers:

7

I'm a python newbie, so please bear with me.

I need to find the frequency of elements in a list

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]

output->

b = [4,4,2,1,2]

Also I want to remove the duplicates from a

a = [1,2,3,4,5]
+6  A: 

For your first question, iterate the list and use a dictionary to keep track of an elements existsence.

For your second question, just use the set operator.

David in Dakota
Can you please elaborate on the first answer
Bruce
+10  A: 

To count the number of appearances:

from collections import defaultdict

appearances = defaultdict(int)

for curr in a:
    appearances[curr] += 1

To remove duplicates:

a = set(a) 
Idan K
+1: `collections.defaultdict`
S.Lott
+1 for collections.defaultdict. Also, in python 3.x, look up collections.Counter. It is the same as collections.defaultdict(int).
hughdbrown
+10  A: 

Counting the frequency of elements is probably best done with a dictionary:

b = {}
for item in a:
    b[item] = b.get(item, 0) + 1

To remove the duplicates, use a set:

a = list(set(a))
lindelof
What's wrong with `collections.defaultdict`?
S.Lott
@S.Lott: What's wrong with posting your answer?
phkahler
@phkahler: Mine would only a tiny bit better than this. It's hardly worth my posting a separate answer when this can be improved with a small change. The point of SO is to get to the *best* answers. I could simply edit this, but I prefer to allow the original author a chance to make their own improvements.
S.Lott
+2  A: 
seta = set(a)
b = [a.count(el) for el in seta]
a = list(seta) #Only if you really want it.
Lakshman Prasad
using lists `count` is ridiculously expensive and uncalled for in this scenario.
Idan K
+12  A: 

Since the list is ordered you can do this:

a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
from itertools import groupby
[len(list(group)) for key, group in groupby(a)]

Output:

[4, 4, 2, 1, 2]
Nadia Alramli
nice, using `groupby`. I wonder about its efficiency vs. the dict approach, though
Eli Bendersky
@Eli, yeah I'm not sure about its efficiency. But it doesn't hurt to have a variety of solutions.
Nadia Alramli
The python groupby creates new groups when the value it sees changes. In this case 1,1,1,2,1,1,1] would return [3,1,3]. If you expected [6,1] then just be sure to sort the data before using groupby.
Evan
I wonder if there's a way to skip the conversion to a list in `len(list(group))`.
Cristian Ciupitu
+2  A: 

In Python 2.7+, you could use collections.Counter to count items

>>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>>
>>> from collections import Counter
>>> c=Counter(a)
>>>
>>> c.values()
[4, 4, 2, 1, 2]
>>>
>>> c.keys()
[1, 2, 3, 4, 5]
S.Mark
+5  A: 

In Python 2.7, you can use collections.Counter:

import collections
a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
counter=collections.Counter(a)
print(counter)
# Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})
print(counter.values())
# [4, 4, 2, 1, 2]
print(counter.keys())
# [1, 2, 3, 4, 5]
print(counter.most_common(3))
# [(1, 4), (2, 4), (3, 2)]

If, like me, you are using Python 2.6 or older, you can download it here.

unutbu