issue174:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue174:python [2021/10/31 23:08] – d52fr | issue174:python [2021/11/02 16:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
Let’s say that List1 contains all of the episodes for a show called “My Life” that have aired so far. And let’s further say that List2 contains the episodes that we have recorded on our home PVR. (The Sxx stands for Season (Series for my friends outside of the US) of the show, and the Exx stands for Episode number.) Just looking at the lists written down, it’s easy to see that List2 (what we have recorded) is missing ‘S01E02’, | Let’s say that List1 contains all of the episodes for a show called “My Life” that have aired so far. And let’s further say that List2 contains the episodes that we have recorded on our home PVR. (The Sxx stands for Season (Series for my friends outside of the US) of the show, and the Exx stands for Episode number.) Just looking at the lists written down, it’s easy to see that List2 (what we have recorded) is missing ‘S01E02’, | ||
+ | |||
+ | Quelque part, je crois avoir abordé le sujet des ensembles dans un article il y a de nombreuses années. Si c'est le cas, je ne pense pas avoir vraiment bien traîté le sujet. J'ai donc décidé d'y remédier ce mois-ci. | ||
+ | |||
+ | Qu' | ||
+ | |||
+ | Supposons que nous ayons deux listes. | ||
+ | |||
+ | List1 = ['' | ||
+ | |||
+ | List2 = [' | ||
+ | |||
+ | Disons que la liste 1 contient tous les épisodes qui ont déjà été diffusés d'une émission intitulée « Ma vie ». Disons également que la liste 2 contient les épisodes que nous avons enregistrés sur notre magnétoscope numérique. (Le Sxx correspond à la saison de la série, et le Exx correspond au numéro de l' | ||
+ | |||
**One solution is to use the in operator, and step through each of the items in List1 and see if the item is in List2. | **One solution is to use the in operator, and step through each of the items in List1 and see if the item is in List2. | ||
Ligne 30: | Ligne 43: | ||
{' | {' | ||
+ | |||
+ | Une solution consiste à utiliser l' | ||
+ | |||
+ | for epi in List1: | ||
+ | if epi not in List2: | ||
+ | print(f ' | ||
+ | |||
+ | print(' | ||
+ | |||
+ | résultat : | ||
+ | Épisode manquant S01E02 | ||
+ | Épisode manquant S01E04 | ||
+ | Épisode manquant S01E06 | ||
+ | Terminé | ||
+ | |||
+ | |||
+ | La méthode not in prend trois lignes (sans compter la définition des listes) pour trouver les épisodes manquants. Nous pouvons cependant faire mieux en utilisant des ensembles. | ||
+ | |||
+ | print(set(List1).difference(set(List2))) | ||
+ | |||
+ | {' | ||
+ | |||
**Using sets, we can do the same thing much quicker, and with only one line of code. The difference method will show all the items from set(a) which are not in set(b). What happens if we reverse the sets in the difference statement, comparing List2 to List1? Since every item in List2 is in List1, we get an empty set returned to us. | **Using sets, we can do the same thing much quicker, and with only one line of code. The difference method will show all the items from set(a) which are not in set(b). What happens if we reverse the sets in the difference statement, comparing List2 to List1? Since every item in List2 is in List1, we get an empty set returned to us. | ||
Ligne 44: | Ligne 79: | ||
You can see that the set is just five items rather than the six we defined, so the duplicate S01E03 is excluded. Also notice that, as we saw in the definition of a set, the order is totally different from what we defined in the list.** | You can see that the set is just five items rather than the six we defined, so the duplicate S01E03 is excluded. Also notice that, as we saw in the definition of a set, the order is totally different from what we defined in the list.** | ||
+ | |||
+ | En utilisant les ensembles, nous pouvons faire la même chose beaucoup plus rapidement et avec une seule ligne de code. La méthode difference montrera tous les éléments de l' | ||
+ | |||
+ | print(set(List2).difference(set(List1)) | ||
+ | |||
+ | set() | ||
+ | |||
+ | Maintenant, qu' | ||
+ | |||
+ | print(set(List2)) | ||
+ | |||
+ | {' | ||
+ | |||
+ | Vous pouvez constater que l' | ||
+ | |||
**You can also use a shortened version of our set difference statement. To do this, we use the - operator. | **You can also use a shortened version of our set difference statement. To do this, we use the - operator. | ||
Ligne 68: | Ligne 118: | ||
SetA={1, | SetA={1, | ||
SetB={5, | SetB={5, | ||
+ | |||
+ | Vous pouvez également utiliser une version abrégée de notre instruction set difference. Pour ce faire, nous utilisons l' | ||
+ | |||
+ | print(set(List1) - set(List2)) | ||
+ | |||
+ | Que pouvons-nous faire d' | ||
+ | |||
+ | set1 = set(List1) | ||
+ | set1.add(' | ||
+ | print(set1) | ||
+ | |||
+ | {' | ||
+ | |||
+ | set2 = set(List2) | ||
+ | set2.discard(' | ||
+ | print(set2) | ||
+ | {' | ||
+ | |||
+ | L' | ||
+ | |||
+ | Nous disposons de nombreuses autres méthodes qui fonctionnent avec les ensembles. Elles incluent l' | ||
+ | |||
+ | SetA={1, | ||
+ | SetB={5, | ||
+ | |||
**Union | **Union | ||
Ligne 88: | Ligne 163: | ||
SetA - SetB** | SetA - SetB** | ||
+ | |||
+ | Union | ||
+ | |||
+ | La méthode union renvoie tous les éléments de l' | ||
+ | |||
+ | SetA.union(SetB) renvoie {1, | ||
+ | |||
+ | Remarquez que la valeur 5 figure dans les deux ensembles, mais comme un ensemble ne peut pas inclure de doublons, elle ne figure qu'une seule fois dans l' | ||
+ | |||
+ | SetA | SetB | ||
+ | |||
+ | Difference | ||
+ | |||
+ | Nous avons déjà vu la méthode difference plus tôt. Elle renvoie les valeurs qui sont dans l' | ||
+ | |||
+ | SetA.difference(SetB) renvoie {1, 2, 3, 4} | ||
+ | |||
+ | Vous pouvez également utiliser l' | ||
+ | |||
+ | SetA - SetB | ||
+ | |||
**Intersection | **Intersection | ||
Ligne 116: | Ligne 212: | ||
5 | 5 | ||
>>> | >>> | ||
+ | |||
+ | Intersection | ||
+ | |||
+ | L' | ||
+ | |||
+ | SetA.intersection(SetB) renvoie {5} | ||
+ | |||
+ | Vous pouvez également utiliser l' | ||
+ | |||
+ | SetA & SetB | ||
+ | |||
+ | Différence symétrique | ||
+ | |||
+ | La méthode de différence symétrique renvoie toutes les valeurs de SetA et SetB qui ne sont pas dans les deux. | ||
+ | |||
+ | SetA.symmetric_difference(SetB) renvoie {1, 2, 3, 4, 6, 7, 8, 9}. | ||
+ | |||
+ | Vous pouvez également utiliser l' | ||
+ | |||
+ | SetA ^ SetB | ||
+ | |||
+ | Les ensembles n'ont pas d' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | |||
+ | 5 | ||
+ | >>> | ||
+ | |||
**If you tried that with a set, the attempt will fail. | **If you tried that with a set, the attempt will fail. | ||
Ligne 137: | Ligne 262: | ||
False | False | ||
>>> | >>> | ||
- | True** | + | True |
- | **However, consider the following situation… | + | However, consider the following situation… |
>>> | >>> | ||
Ligne 149: | Ligne 274: | ||
SetC is not a subset of SetA, since SetC contains the value of 10 and SetA does not. In the same way SetA is not a superset of SetC.** | SetC is not a subset of SetA, since SetC contains the value of 10 and SetA does not. In the same way SetA is not a superset of SetC.** | ||
+ | |||
+ | Si vous essayez cela avec un ensemble, la tentative échouera. | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | Traceback (most recent call last): | ||
+ | File "< | ||
+ | TypeError: ' | ||
+ | |||
+ | ' | ||
+ | >>> | ||
+ | |||
+ | Il existe également les méthodes issuperset() et issubset(). SetA est considéré comme un sur-ensemble (superset) s'il contient tous les éléments de SetB. L' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | True # Vrai | ||
+ | >>> | ||
+ | False # Faux | ||
+ | >>> | ||
+ | True | ||
+ | |||
+ | Cependant, considérez la situation suivante : | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | Faux | ||
+ | >>> | ||
+ | False | ||
+ | >>> | ||
+ | |||
+ | SetC n'est pas un sous-ensemble de SetA, car SetC contient la valeur 10 et SetA ne la contient pas. De la même manière, SetA n'est pas un sur-ensemble de SetC. | ||
+ | |||
**Frozenset | **Frozenset | ||
Ligne 179: | Ligne 338: | ||
Until next time, as always; stay safe, healthy, positive and creative!** | Until next time, as always; stay safe, healthy, positive and creative!** | ||
+ | |||
+ | Frozenset | ||
+ | |||
+ | Un frozenset est une classe qui possède les mêmes caractéristiques qu'un ensemble, mais un frozenset est immuable et il n'y a donc pas de méthodes d' | ||
+ | |||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | frozenset({1, | ||
+ | |||
+ | >>> | ||
+ | frozenset({1, | ||
+ | |||
+ | >>> | ||
+ | frozenset({6}) | ||
+ | |||
+ | >>> | ||
+ | frozenset({1, | ||
+ | |||
+ | >>> | ||
+ | Traceback (most recent call last): | ||
+ | File "< | ||
+ | AttributeError: | ||
+ | |||
+ | ' | ||
+ | >>> | ||
+ | |||
+ | Il y a beaucoup d' | ||
+ | |||
+ | Jusqu' | ||
+ | |||
issue174/python.1635718133.txt.gz · Dernière modification : 2021/10/31 23:08 de d52fr