Python的新功能。
我正在寻找一种创建列表(输出)的简单方法,该方法可以在保留索引(?)的同时返回另一个目标列表(MyList)的元素计数。
这就是我想要得到的:
MyList = ["a", "b", "c", "c", "a", "c"]
Output = [ 2 , 1 , 3 , 3 , 2 , 3 ]
我找到了解决类似问题的方法。计算列表中每个元素的出现次数。
In : Counter(MyList)
Out : Counter({'a': 2, 'b': 1, 'c': 3})
但是,这将返回一个不保留索引的Counter对象。
我假设给定了计数器中的键,我可以构造所需的输出,但是我不确定如何进行操作。
其他信息,我在脚本中导入了熊猫,而MyList实际上是熊猫数据框中的一列。
答案 0 :(得分:10)
您可以使用函数itemgetter()
来代替其他解决方案中的listcomp:
from collections import Counter
from operator import itemgetter
MyList = ["a", "b", "c", "c", "a", "c"]
c = Counter(MyList)
itemgetter(*MyList)(c)
# (2, 1, 3, 3, 2, 3)
更新:正如@ALollz在评论中提到的,此解决方案似乎是最快的解决方案。如果OP需要列表而不是元组,则结果必须转换为list()
。
答案 1 :(得分:5)
您可以使用list.count
方法,该方法将计算每个字符串在MyList
中发生的次数。您可以使用list comprehension来生成带有计数的新列表:
MyList = ["a", "b", "c", "c", "a", "c"]
[MyList.count(i) for i in MyList]
# [2, 1, 3, 3, 2, 3]
答案 2 :(得分:5)
使用np.unique
创建一个值计数字典并映射值。这将很快,尽管不如Counter方法快:
import numpy as np
list(map(dict(zip(*np.unique(MyList, return_counts=True))).get, MyList))
#[2, 1, 3, 3, 2, 3]
一些中等大小列表的时间:
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)
更大的列表:
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)
答案 3 :(得分:3)
您只需要实现以下代码
c=Counter(MyList)
lout=[c[i] for i in MyList]
现在列表 lout 是您想要的输出
答案 4 :(得分:3)
熊猫解决方案如下:
df = pd.DataFrame(data=["a", "b", "c", "c", "a", "c"], columns=['MyList'])
df['Count'] = df.groupby('MyList')['MyList'].transform(len)
编辑:如果这是您唯一想做的事,那就不要使用熊猫。我只是因为熊猫标签回答了这个问题。
性能取决于组数:
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)
答案 5 :(得分:1)
请注意,@ Gio指示该列表是pandas Series对象。在这种情况下,您可以将Series对象转换为列表:
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]
但是,一旦有了系列,就可以通过jms serializer对元素进行计数。
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]
答案 6 :(得分:0)
这是hettinger的经典片段之一:)
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