CAPSLOCK
CAPSLOCK

Reputation: 6483

List elements’ counter

New to Python here.

I am looking for a simple way of creating a list (Output), which returns the count of the elements of another objective list (MyList) while preserving the indexing(?).

This is what I would like to get:

MyList = ["a", "b", "c", "c", "a", "c"]
Output = [ 2 ,  1 ,  3 ,  3 ,  2 ,  3 ]

I found solutions to a similar problem. Count the number of occurrences for each element in a list.

In  : Counter(MyList)
Out : Counter({'a': 2, 'b': 1, 'c': 3})

This, however, returns a Counter object which doesn't preserve the indexing.

I assume that given the keys in the Counter I could construct my desired output, however I am not sure how to proceed.

Extra info, I have pandas imported in my script and MyList is actually a column in a pandas dataframe.

Upvotes: 9

Views: 675

Answers (7)

Mykola Zotko
Mykola Zotko

Reputation: 17824

Instead of listcomp as in another solution you can use the function itemgetter:

from collections import Counter
from operator import itemgetter

lst = ["a", "b", "c", "c", "a", "c"]

c = Counter(lst)
itemgetter(*lst)(c)
# (2, 1, 3, 3, 2, 3)

UPDATE: As @ALollz mentioned in the comments this solution seems to be the fastet one. If OP needs a list instead of a tuple the result must be converted wih list.

Upvotes: 11

prosti
prosti

Reputation: 46351

Note there was the indication from @Gio that list was pandas Series object. In that case you can convert Series object to list:

import pandas as pd

l = ["a", "b", "c", "c", "a", "c"]
ds = pd.Series(l) 
l=ds.tolist()
[l.count(i) for i in ds] 
# [2, 1, 3, 3, 2, 3]

But, once you have the Series, you can count the elements via value_counts.

l = ["a", "b", "c", "c", "a", "c"]
s = pd.Series(l) #Series object
c=s.value_counts() #c is Series again
[c[i] for i in s] 
# [2, 1, 3, 3, 2, 3]

Upvotes: 1

JoergVanAken
JoergVanAken

Reputation: 1286

A pandas solution looks like this:

df = pd.DataFrame(data=["a", "b", "c", "c", "a", "c"], columns=['MyList'])
df['Count'] = df.groupby('MyList')['MyList'].transform(len)

Edit: One shouldn't use pandas if this is the only thing you want to do. I only answered this question because of the pandas tag.

The performance depends on the number of groups:

MyList = np.random.randint(1, 10, 10000).tolist()
df = pd.DataFrame(MyList)

%timeit [MyList.count(i) for i in MyList]
# 1.32 s ± 15.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit df.groupby(0)[0].transform(len)
# 3.89 ms ± 112 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

MyList = np.random.randint(1, 9000, 10000).tolist()
df = pd.DataFrame(MyList)

%timeit [MyList.count(i) for i in MyList]
# 1.36 s ± 11.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit df.groupby(0)[0].transform(len)
# 1.33 s ± 19.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Upvotes: 3

ALollz
ALollz

Reputation: 59549

Use np.unique to create a dictionary of value counts and map the values. This will be fast, though not as fast as the Counter methods:

import numpy as np

list(map(dict(zip(*np.unique(MyList, return_counts=True))).get, MyList))
#[2, 1, 3, 3, 2, 3]

Some timings for a moderate sized list:

MyList = np.random.randint(1, 2000, 5000).tolist()

%timeit [MyList.count(i) for i in MyList]
#413 ms ± 165 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit list(map(dict(zip(*np.unique(MyList, return_counts=True))).get, MyList))
#1.89 ms ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit pd.DataFrame(MyList).groupby(MyList).transform(len)[0].tolist()
#2.18 s ± 12.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

c=Counter(MyList)
%timeit lout=[c[i] for i in MyList]
#679 µs ± 2.33 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

c = Counter(MyList)
%timeit list(itemgetter(*MyList)(c))
#503 µs ± 162 ns per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Larger list:

MyList = np.random.randint(1, 2000, 50000).tolist()

%timeit [MyList.count(i) for i in MyList]
#41.2 s ± 5.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit list(map(dict(zip(*np.unique(MyList, return_counts=True))).get, MyList))
#18 ms ± 56.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit pd.DataFrame(MyList).groupby(MyList).transform(len)[0].tolist()
#2.44 s ± 12.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

c=Counter(MyList)
%timeit lout=[c[i] for i in MyList]
#6.89 ms ± 22.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

c = Counter(MyList)
%timeit list(itemgetter(*MyList)(c))
#5.27 ms ± 10.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Upvotes: 5

yatu
yatu

Reputation: 88236

You can use the list.count method, which will count the amount of times each string takes place in MyList. You can generate a new list with the counts by using a list comprehension:

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

[MyList.count(i) for i in MyList]
# [2, 1, 3, 3, 2, 3]

Upvotes: 5

han solo
han solo

Reputation: 6590

This is one from the hettinger's classic snippets :)

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
     'Counter that remembers the order elements are first seen'
     def __repr__(self):
         return '%s(%r)' % (self.__class__.__name__,
                            OrderedDict(self))
     def __reduce__(self):
         return self.__class__, (OrderedDict(self),)

x = ["a", "b", "c", "c", "a", "c"]
oc = OrderedCounter(x)
>>> oc
OrderedCounter(OrderedDict([('a', 2), ('b', 1), ('c', 3)]))
>>> oc['a']
2

Upvotes: 0

akhil pathirippilly
akhil pathirippilly

Reputation: 1060

You just need to implement below piece of code

    c=Counter(MyList)
    lout=[c[i] for i in MyList]

now list lout is your desired output

Upvotes: 3

Related Questions