How do you retain negative discrepancies in a sample inventory?

Counter.subtract updates tallies in place and retains zero or negative balances.

Python example and interpretation

Expecting two A records and receiving three leaves minus one. A multiset difference retaining only positive counts would conceal the surplus.

from collections import Counter
c=Counter(A=2,B=1)
c.subtract({'A':3,'B':1})
dict(c)

Result {'A': -1, 'B': 0}

中文

怎样保留盘点后为负的样本数量差?

Counter.subtract 原地减去另一组计数,零和负数都可以留下。

Python 算例与适用条件

预期有两条 A,实际收到三条,差额为 −1。若只保存正计数的集合差,超收的信号便会消失。

from collections import Counter
c=Counter(A=2,B=1)
c.subtract({'A':3,'B':1})
dict(c)

结果 {'A': -1, 'B': 0}

References / 参考来源

Keep reading / 继续阅读