2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -10,7 +10,7 @@
In [1]:
```
```py
a = set()
type(a)
@@ -18,7 +18,7 @@ type(a)
Out[1]:
```
```py
set
```
@@ -26,7 +26,7 @@ set
In [2]:
```
```py
a = set([1, 2, 3, 1])
a
@@ -34,7 +34,7 @@ a
Out[2]:
```
```py
{1, 2, 3}
```
@@ -44,7 +44,7 @@ Out[2]:
In [3]:
```
```py
a = {1, 2, 3, 1}
a
@@ -52,7 +52,7 @@ a
Out[3]:
```
```py
{1, 2, 3}
```
@@ -60,7 +60,7 @@ Out[3]:
In [4]:
```
```py
s = {}
type(s)
@@ -68,7 +68,7 @@ type(s)
Out[4]:
```
```py
dict
```
@@ -78,7 +78,7 @@ dict
In [5]:
```
```py
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
@@ -92,40 +92,40 @@ b = {3, 4, 5, 6}
In [6]:
```
```py
a.union(b)
```
Out[6]:
```
```py
{1, 2, 3, 4, 5, 6}
```
In [7]:
```
```py
b.union(a)
```
Out[7]:
```
```py
{1, 2, 3, 4, 5, 6}
```
In [8]:
```
```py
a | b
```
Out[8]:
```
```py
{1, 2, 3, 4, 5, 6}
```
@@ -137,51 +137,51 @@ Out[8]:
In [9]:
```
```py
a.intersection(b)
```
Out[9]:
```
```py
{3, 4}
```
In [10]:
```
```py
b.intersection(a)
```
Out[10]:
```
```py
{3, 4}
```
In [11]:
```
```py
a & b
```
Out[11]:
```
```py
{3, 4}
```
In [12]:
```
```py
print(a & b)
```
```
```py
set([3, 4])
```
@@ -196,27 +196,27 @@ set([3, 4])
In [13]:
```
```py
a.difference(b)
```
Out[13]:
```
```py
{1, 2}
```
In [14]:
```
```py
a - b
```
Out[14]:
```
```py
{1, 2}
```
@@ -224,27 +224,27 @@ Out[14]:
In [15]:
```
```py
b.difference(a)
```
Out[15]:
```
```py
{5, 6}
```
In [16]:
```
```py
b - a
```
Out[16]:
```
```py
{5, 6}
```
@@ -256,40 +256,40 @@ Out[16]:
In [17]:
```
```py
a.symmetric_difference(b)
```
Out[17]:
```
```py
{1, 2, 5, 6}
```
In [18]:
```
```py
b.symmetric_difference(a)
```
Out[18]:
```
```py
{1, 2, 5, 6}
```
In [19]:
```
```py
a ^ b
```
Out[19]:
```
```py
{1, 2, 5, 6}
```
@@ -299,7 +299,7 @@ Out[19]:
In [20]:
```
```py
a = {1, 2, 3}
b = {1, 2}
@@ -309,27 +309,27 @@ b = {1, 2}
In [21]:
```
```py
b.issubset(a)
```
Out[21]:
```
```py
True
```
In [22]:
```
```py
b <= a
```
Out[22]:
```
```py
True
```
@@ -337,27 +337,27 @@ True
In [23]:
```
```py
a.issuperset(b)
```
Out[23]:
```
```py
True
```
In [24]:
```
```py
a >= b
```
Out[24]:
```
```py
True
```
@@ -365,14 +365,14 @@ True
In [25]:
```
```py
a <= a
```
Out[25]:
```
```py
True
```
@@ -380,14 +380,14 @@ True
In [26]:
```
```py
a < a
```
Out[26]:
```
```py
False
```
@@ -397,7 +397,7 @@ False
跟列表的 `append` 方法类似,用来向集合添加单个元素。
```
```py
s.add(a)
```
@@ -405,7 +405,7 @@ s.add(a)
In [27]:
```
```py
t = {1, 2, 3}
t.add(5)
t
@@ -414,7 +414,7 @@ t
Out[27]:
```
```py
{1, 2, 3, 5}
```
@@ -422,7 +422,7 @@ Out[27]:
In [28]:
```
```py
t.add(3)
t
@@ -430,7 +430,7 @@ t
Out[28]:
```
```py
{1, 2, 3, 5}
```
@@ -438,7 +438,7 @@ Out[28]:
跟列表的`extend`方法类似,用来向集合添加多个元素。
```
```py
s.update(seq)
```
@@ -446,7 +446,7 @@ s.update(seq)
In [29]:
```
```py
t.update([5, 6, 7])
t
@@ -454,13 +454,13 @@ t
Out[29]:
```
```py
{1, 2, 3, 5, 6, 7}
```
### `remove` 方法移除单个元素
```
```py
s.remove(ob)
```
@@ -468,7 +468,7 @@ s.remove(ob)
In [30]:
```
```py
t.remove(1)
t
@@ -476,18 +476,18 @@ t
Out[30]:
```
```py
{2, 3, 5, 6, 7}
```
In [31]:
```
```py
t.remove(10)
```
```
```py
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-31-3bc25c5e1ff4> in <module>()
@@ -502,39 +502,39 @@ KeyError: 10
In [32]:
```
```py
t.pop()
```
Out[32]:
```
```py
{3, 5, 6, 7}
```
In [33]:
```
```py
print t
```
```
```py
set([3, 5, 6, 7])
```
In [34]:
```
```py
s = set()
# 报错
s.pop()
```
```
```py
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-34-9f9e06c962e6> in <module>()
@@ -551,21 +551,21 @@ KeyError: 'pop from an empty set'
In [35]:
```
```py
t.discard(3)
```
In [36]:
```
```py
t
```
Out[36]:
```
```py
{5, 6, 7}
```
@@ -573,27 +573,27 @@ Out[36]:
In [37]:
```
```py
t.discard(20)
```
In [38]:
```
```py
t
```
Out[38]:
```
```py
{5, 6, 7}
```
### difference_update方法
```
```py
a.difference_update(b)
```