# Counter as Multiset and includes

**URL:** <https://discuss.python.org/t/counter-as-multiset-and-includes/70340>\
**Category:** Documentation\
**Created:** [November 6, 2024, 12:28pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340 "2024-11-06T12:28:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paddy3118](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/paddy3118/32/1396_2.png) [@Paddy3118](https://discuss.python.org/u/Paddy3118)\
**Post date:** [November 6, 2024, 12:28pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/1 "2024-11-06T12:28:46Z")

</div>

I am confused by [the operator shown for multiset includes](https://docs.python.org/3/library/collections.html#collections.Counter:~:text=c%20%3C%3D%20d-,%23%20inclusion%3A,-c%5Bx%5D%20%3C%3D%20d) because if talking about calculating if `A:multiset includes B:multiset` using Counters as multisets, you need to write `A >= B`.

The documentation seems to do B is included in A if `B <= A` which may well be correct **but** it has the order of the two names flipped, and there might be confusion because in flipping what is currently written, one might be tempted to use `A > B` instead of `A >= B`

The examples show:

1. c add d
2. c subtract d
3. c intersect d
4. c union d
5. c equal d
6. d included in c. # Wrong way round?

## My tests

```python
from collections import Counter
from itertools import product

# For testing
ab = [({'x':x, 'y': y, 'z': z}, {'x':x1, 'y': y1, 'z': z1}) 
      for x, y, z, x1, y1, z1 in product([0, 1, 2, 3], repeat=6)]
#%%
"""
## Inclusion

A includes B

This is the same as Counter(A) >= Counter(B)
"""

def includes(A, B):
    return (set(A.keys()).issuperset(set(B.keys()))
            and all(A[b_key] >= b_val for b_key, b_val in B.items()))

for A, B in ab:
    assert (c:=(Counter(A)) >= Counter(B)) == (i:=includes(A, B)), f"{A,B, c, i = }"

```

Ref: [Multiset - Wikipedia](https://en.wikipedia.org/wiki/Multiset#Basic_properties_and_operations)

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [November 6, 2024, 12:42pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/2 "2024-11-06T12:42:31Z")

</div>

I read `c <= d` as “`c` is included by/in `d`”

---

<div class="post-metadata">

**Author:** ![Paddy3118](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/paddy3118/32/1396_2.png) [@Paddy3118](https://discuss.python.org/u/Paddy3118)\
**Post date:** [November 6, 2024, 1:35pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/3 "2024-11-06T13:35:27Z")

</div>

\oh! I just re-read the docs as well as Wikipedia and i am confused by thoughts of X Y mapping naturally to adds, subtracts, intersects, unions(?), equals, _includes_.

_Includes_ seems more part of the progression than a substitution of “_included by_”

It seems the word stem include has a different meaning when followed with “by” compared to the others?

I find it confusing, still.

---

<div class="post-metadata">

**Author:** ![Nodd](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nodd/32/6805_2.png) [@Nodd](https://discuss.python.org/u/Nodd)\
**Post date:** [November 6, 2024, 1:51pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/4 "2024-11-06T13:51:54Z")

</div>

If I understand your confusion correctly, it’s because “is included” is the passive form of “includes”, so it reverses the roles of the subject and the complement. So “A includes B” is the same as “B is included in A”. This way, A is “bigger” than B, so the notation `B <= A` is correct.

---

<div class="post-metadata">

**Author:** ![Paddy3118](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/paddy3118/32/1396_2.png) [@Paddy3118](https://discuss.python.org/u/Paddy3118)\
**Post date:** [November 6, 2024, 2:19pm UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/5 "2024-11-06T14:19:02Z")

</div>

Yep, that reversal got me!

---

<div class="post-metadata">

**Author:** ![Paddy3118](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/paddy3118/32/1396_2.png) [@Paddy3118](https://discuss.python.org/u/Paddy3118)\
**Post date:** [November 7, 2024, 9:57am UTC](https://discuss.python.org/t/counter-as-multiset-and-includes/70340/6 "2024-11-07T09:57:16Z")

</div>

I’ve created this markdown table in my docs to help me out:

| Mset Operation | Counter() exp | Comment |
| --- | --- | --- |
| A includes B | A \>= B | → Bool; A \>= A == True |
| A included-by B | A \<= B | → Bool; A \<= A == True |
| A union B | A | B | → Counter |
| A intersect B | A & B | → Counter |
| A sum B | A + B | → Counter |
| A difference B | A - B | → Counter |
