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

Contenu supprimé Contenu ajouté
Ligne 142 :
</source>
 
== Opérations sur les ensembles ==
==Set Operations==
 
Python offre les mêmes opérations sur les ensembles qu'en mathématiques, applicables par soit par des opérateurs, soit par des fonctions équivalentes.
Python allows us to perform all the standard mathematical set operations, using members of set. Note that each of these set operations has several forms. One of these forms, s1.function(s2) will return another set which is created by "function" applied to <math>S_1</math> and <math>S_2</math>. The other form, s1.function_update(s2), will change <math>S_1</math> to be the set created by "function" of <math>S_1</math> and <math>S_2</math>. Finally, some functions have equivalent special operators. For example, s1 & s2 is equivalent to s1.intersection(s2)
 
=== Intersection ===
Les éléments communs à deux ensembles.
 
Any element which is in both <math>S_1</math> and <math>S_2</math> will appear in their [[w:intersection_(set_theory)|intersection]].
 
<source lang="python">
Ligne 157 ⟶ 156 :
>>> s1 & s2
set([6])
 
>>> s1.intersection_update(s2)
>>> s1
Ligne 162 :
</source>
 
=== Union ===
Somme des éléments de deux ensembles.
 
The [[w:union_(set_theory)|union]] is the merger of two sets. Any element in <math>S_1</math> or <math>S_2</math> will appear in their union.
 
<source lang="python">
Ligne 175 ⟶ 174 :
</source>
 
=== Différence symétrique ===
Note that union's update function is simply "update" [[#Constructing_Sets|above]].
 
===Symmetric Difference===
 
The [[w:symmetric_difference|symmetric difference]] of two sets is the set of elements which are in one of either set, but not in both.
Ligne 193 ⟶ 190 :
</source>
 
===Set DifferenceDifférence ===
 
Python can also find the [[w:Complement_(set_theory)#Relative_Complement|set difference]] of <math>S_1</math> and <math>S_2</math>, which is the elements that are in <math>S_1</math> but not in <math>S_2</math>.