15-03-2019
This commit is contained in:
131
docs/Programmation/Python/set.md
Normal file
131
docs/Programmation/Python/set.md
Normal file
@@ -0,0 +1,131 @@
|
||||
# Set
|
||||
|
||||
|
||||
|
||||
Une liste est une collection **non ordonnée** et **non indexée**. On ne peut pas modifier un item, mais on peut en ajouter.
|
||||
|
||||
|
||||
|
||||
##### Créer un set:
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set
|
||||
{'alpha', 'beta', 'gamma'}
|
||||
```
|
||||
|
||||
```python
|
||||
# Constructor set()
|
||||
>>> un_set = set(("alpha", "beta", "gamma"))
|
||||
>>> un_set
|
||||
{'gamma', 'alpha', 'beta'}
|
||||
```
|
||||
|
||||
##### Ajouter un item:
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set.add("delta")
|
||||
>>> un_set
|
||||
{'gamma', 'alpha', 'beta', 'delta'}
|
||||
```
|
||||
|
||||
##### Ajouter plusieurs items:
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set.update(["delta","zeta","epsilon"])
|
||||
>>> un_set
|
||||
{'zeta', 'alpha', 'beta', 'delta', 'gamma', 'epsilon'}
|
||||
```
|
||||
|
||||
##### Longueur d'un set:
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> print(len(un_set))
|
||||
3
|
||||
```
|
||||
|
||||
##### Supprimer un item (remove):
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set.remove("beta")
|
||||
>>> un_set
|
||||
{'gamma', 'alpha'}
|
||||
```
|
||||
|
||||
##### Supprimer un item (discard):
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set.discard("beta")
|
||||
>>> un_set
|
||||
{'gamma', 'alpha'}
|
||||
```
|
||||
|
||||
##### Vider un set:
|
||||
|
||||
```python
|
||||
>>> un_set = {"alpha", "beta", "gamma"}
|
||||
>>> un_set.clear()
|
||||
>>> un_set
|
||||
set()
|
||||
```
|
||||
|
||||
##### Supprimer un set:
|
||||
|
||||
```python
|
||||
>>> del un_set
|
||||
>>> un_set
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
NameError: name 'un_set' is not defined
|
||||
```
|
||||
|
||||
##### Boucle:
|
||||
|
||||
```python
|
||||
un_set = {"alpha", "beta", "gamma"}
|
||||
for x in un_set:
|
||||
print(x)
|
||||
|
||||
alpha
|
||||
beta
|
||||
gamma
|
||||
```
|
||||
|
||||
##### Test si un item est présent dans le set:
|
||||
|
||||
```python
|
||||
un_set = {"alpha", "beta", "gamma"}
|
||||
print("alpha" in un_set)
|
||||
|
||||
True
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### Méthodes:
|
||||
|
||||
| Méthodes | Description |
|
||||
| ----------------------------- | ----------- |
|
||||
| add() | |
|
||||
| clear() | |
|
||||
| copy() | |
|
||||
| difference() | |
|
||||
| difference_update() | |
|
||||
| discard() | |
|
||||
| intersection() | |
|
||||
| intersection_update() | |
|
||||
| isdisjoint() | |
|
||||
| issubset() | |
|
||||
| issuperset() | |
|
||||
| pop() | |
|
||||
| remove() | |
|
||||
| symmetric_difference() | |
|
||||
| symmetric_difference_update() | |
|
||||
| union() | |
|
||||
| update() | |
|
||||
|
||||
Reference in New Issue
Block a user