python大列表减去小列表_在Python中减去两个列表

本文介绍了Python中如何使用collections.Counter类进行列表之间的减法操作。通过示例展示了如何创建Counter对象,以及如何使用Counter的update、most_common、elements等方法。最后,通过一个具体的例子演示了如何从一个Counter对象中减去另一个Counter对象,并获取结果。
摘要由CSDN通过智能技术生成

ephemient..

5

Python 2.7+和3.0有collections.Counter(又名multiset).文档链接到Recipe 576611: Python 2.5的Counter类:

from operator import itemgetter

from heapq import nlargest

from itertools import repeat, ifilter

class Counter(dict):

'''Dict subclass for counting hashable objects. Sometimes called a bag

or multiset. Elements are stored as dictionary keys and their counts

are stored as dictionary values.

>>> Counter('zyzygy')

Counter({'y': 3, 'z': 2, 'g': 1})

'''

def __init__(self, iterable=None, **kwds):

'''Create a new, empty Counter object. And if given, count elements

from an input iterable. Or, initialize the count from another mapping

of elements to their counts.

>>> c = Counter() # a new, empty counter

>>> c = Counter('gallahad') # a new counter from an iterable

>>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping

>>> c = Counter(a=4, b=2) # a new counter from keyword args

'''

self.update(iterable, **kwds)

def __missing__(self, key):

return 0

def most_common(self, n=None):

'''List the n most common elements an

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值