4

我有一个Counter(from collections) 并且想过滤掉一组不需要的项目。结果应该是一个新的 Counter (或者如果你喜欢,就地做),只包含与属性不匹配的项目。我尝试filter在 the 上使用,Counter但结果Counter不再是 a 而只是list. 我还尝试从中减去一个set不需要的项目,Counter但该操作没有实现。减去一个Counter作品,但我没有第二个Counter,创建它本质上是我正在尝试执行的相同任务。

Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4 ])
→ Counter({4: 6, 5: 5, 3: 4, 6: 3, 2: 2, 1: 1, 7: 1})

现在我想从这个计数器中删除所有23值,所以结果应该是

Counter({4: 6, 5: 5, 6: 3, 1: 1, 7: 1})

这是我的方法:

filter(lambda x: x not in (2, 3), c)
→ [1, 4, 5, 6, 7]

但我不想要一个清单。

c - set([ 2, 3 ])
→ TypeError: unsupported operand type(s) for -: 'Counter' and 'set'

我可以使用 sth 来迭代解压缩的元素列表,Counter如下所示:

Counter(x for x in c.elements() if x not in (2, 3))
→ Counter({4: 6, 5: 5, 6: 3, 1: 1, 7: 1})

但这显然对于大笔交易来说是不必要的昂贵。

我发现的唯一(不是很好)解决方案是这样的麻烦:

Counter({ k: v for k, v in c.iteritems() if k not in (2, 3) })

有没有更好、更容易、更易读的东西我忽略了?

为什么没有一个Counter可以与 a 一起使用的已实现的减法运算符set

4

3 回答 3

3

只需使用del

>>> c = Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4 ])
>>> c
Counter({4: 6, 5: 5, 3: 4, 6: 3, 2: 2, 1: 1, 7: 1})
>>> del c[2]
>>> del c[3]
>>> c
Counter({4: 6, 5: 5, 6: 3, 1: 1, 7: 1})
>>>

只是为了好玩,您可以Counter用较大的值减去另一个要删除的键,但最好坚持使用del

>>> c = Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4 ])
>>> c
Counter({4: 6, 5: 5, 3: 4, 6: 3, 2: 2, 1: 1, 7: 1})
>>> c - Counter({2:sys.maxint, 3:sys.maxint})
Counter({4: 6, 5: 5, 6: 3, 1: 1, 7: 1})
于 2013-09-04T12:41:10.743 回答
1

尝试这个:

from collections import Counter
c=Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4 ])
c2=Counter()
for x in c.most_common():
    if x[1]<2 or x[1]>3:
        c2[x[0]]+=x[1]
print(c2)
于 2018-01-04T14:26:03.787 回答
0

您可以使用 pop - 它比使用 del 或字典理解更快。

def alt():
    C = Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4])
    for k in C.keys():
        if k in (2, 3):
            del C[k]

def alt2():
    C = Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4])
    for k in C.keys():
        if k in (2, 3):
            C.pop(k)

def alt3():
    C = Counter([ 1,2,3,4,5,6,7,6,5,4,3,2,3,4,5,6,5,4,3,4,5,4])
    Counter({ k: v for k, v in c.iteritems() if k not in (2, 3) })

蟒蛇:

>>> %timeit alt()
100000 loops, best of 3: 9.66 µs per loop

>>> %timeit alt2()
100000 loops, best of 3: 8.64 µs per loop

>>> %timeit alt3()
100000 loops, best of 3: 11.3 µs per loop
于 2017-12-03T22:00:30.833 回答