I'll soon forget about the set
--A set is a collection of unordered elements that have no overlapping elements. -Generated by placing elements inside {} separated by commas or by passing a sequence type to the set function
Operation type | Description |
---|---|
Sum (union) | Included in A or B |
Product (intersection) | Common to A and B |
Difference | Included only in A |
Symmetric difference | Included only in A or B |
>>> {1, 2, 3} | {2, 3, 4} #sum
{1, 2, 3, 4}
Or
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.union(b)
{1, 2, 3, 4}
When changing the original set
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.update(b)
{1, 2, 3, 4}
>>> a
{1, 2, 3, 4}
>>> {1, 2, 3} & {2, 3, 4} #product
{2, 3}
Or
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.intersection(b)
{2, 3}
When changing the original set
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.intersection_update(b)
>>> a
{2, 3}
>>> {1, 2, 3} - {2, 3, 4} #difference
{1}
Or
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.difference(b)
{1}
When changing the original set
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.difference_update(b)
>>> a
{1}
>>> {1, 2, 3} ^ {2, 3, 4} #Target difference
{1, 4}
Or
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.symmetric_difference(b)
{1, 4}
When changing the original set
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a.symmetric_difference_update(b)
>>> a
{1, 4}
>>> {1, 3} <= {1, 2, 3}
True
>>> {1, 4} <= {1, 2, 3}
False
Enjoy Python ~
Recommended Posts