>>> a={1,2,2,3,4,4,5,6}
>>> a
{1, 2, 3, 4, 5, 6}
>>> type(a)
<class'set'>
>>> b={2,3,3,6,7}
>>> b
{2, 3, 6, 7}
>>> a-b
{1, 4, 5}
>>> b-a
{7}
>>> a & b
{2, 3, 6}
>>> a | b
{1, 2, 3, 4, 5, 6, 7}
>>> a ^ b
{1, 4, 5, 7}
>>> s={1,2,3,4,5}
>>> s
{1, 2, 3, 4, 5}
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'set' object is not subscriptable
>>> s.add(6)
>>> s
{1, 2, 3, 4, 5, 6}
>>> s.add(6)
>>> s
{1, 2, 3, 4, 5, 6}
>>> s.remove(6)
>>> s
{1, 2, 3, 4, 5}
>>> s.clear()
>>> s
set()
>>> f=['apple', 'banana', 'apple', 'banana']
>>> f
['apple', 'banana', 'apple', 'banana']
>>> set(f)
{'banana', 'apple'}
>>> kind=set(f)
>>> kind
{'banana', 'apple'}
>>>
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme