MCPcopy Index your code
hub / github.com/RustPython/RustPython / subtract

Method subtract

Lib/collections/__init__.py:714–739  ·  view source on GitHub ↗

Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts. Source can be an iterable, a dictionary, or another Counter instance. >>> c = Counter('

(self, iterable=None, /, **kwds)

Source from the content-addressed store, hash-verified

712 self.update(kwds)
713
714 def subtract(self, iterable=None, /, **kwds):
715 '''Like dict.update() but subtracts counts instead of replacing them.
716 Counts can be reduced below zero. Both the inputs and outputs are
717 allowed to contain zero and negative counts.
718
719 Source can be an iterable, a dictionary, or another Counter instance.
720
721 >>> c = Counter('which')
722 >>> c.subtract('witch') # subtract elements from another iterable
723 >>> c.subtract(Counter('watch')) # subtract elements from another counter
724 >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
725 0
726 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
727 -1
728
729 '''
730 if iterable is not None:
731 self_get = self.get
732 if isinstance(iterable, _collections_abc.Mapping):
733 for elem, count in iterable.items():
734 self[elem] = self_get(elem, 0) - count
735 else:
736 for elem in iterable:
737 self[elem] = self_get(elem, 0) - 1
738 if kwds:
739 self.subtract(kwds)
740
741 def copy(self):
742 'Return a shallow copy.'

Callers 2

test_subtractMethod · 0.95

Calls 2

isinstanceFunction · 0.85
itemsMethod · 0.45

Tested by 2

test_subtractMethod · 0.76