« Programmation Python/Ensembles » : différence entre les versions

Contenu supprimé Contenu ajouté
Ligne 73 :
</source>
 
== Recherche de membre ==
==Membership Testing==
 
We can check if an object is in the set using the same "in" operator as with sequential data types.
 
Pour chercher si un élément existe dans un ensemble, on utilise "in" :
<source lang="python">
>>> 32 in s
Ligne 86 ⟶ 85 :
</source>
 
Si un sous-ensemble existe dans un ensemble, c'est "issubset()" :
We can also test the membership of entire sets. Given two sets <math>S_1</math> and <math>S_2</math>, we check if <math>S_1</math> is a [[w:Subset|subset]] or a superset of <math>S_2</math>.
 
<source lang="python">
>>> s.issubset(set([32, 8, 9, 12, 14, -4, 54, 26, 19]))
True
>>> s.issuperset(set([9, 12]))
True
</source>
 
Si un sur-ensemble contient un ensemble, c'est "issuperset()" :
Note that "issubset" and "issuperset" can also accept sequential data types as arguments
 
<source lang="python">
>>> s.issuperset(set([329, 912]))
True
</source>
 
# Équivalent à :
Note that the <= and >= operators also express the issubset and issuperset functions respectively.
>>> s.issuperset(set([9, 12]))
 
# Équivalent à :
<source lang="python">
>>> set([4,s 5, 7]) <>= set([49, 5, 7, 912])
True
>>> set([9, 12, 15]) >= set([9, 12])
True
</source>
 
Like lists, tuples, and string, we can use the "len" function to find the number of items in a set.
 
==Removing Items==