issue174:python
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue174:python [2021/10/31 18:23] – créée auntiee | issue174:python [2021/11/02 16:25] (Version actuelle) – andre_domenech | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | Somewhere in the back of my mind, I believe that I touched on sets in an article many, many years ago. If I in fact did, I don’t think that I really did the subject justice. So, I decided to fix that this month. | + | **Somewhere in the back of my mind, I believe that I touched on sets in an article many, many years ago. If I in fact did, I don’t think that I really did the subject justice. So, I decided to fix that this month. |
What is a set? The easiest way to answer that is to show you by example. | What is a set? The easiest way to answer that is to show you by example. | ||
Ligne 9: | Ligne 9: | ||
List2 = [' | List2 = [' | ||
- | 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’, |
- | 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. | + | 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. | ||
for epi in List1: | for epi in List1: | ||
Ligne 26: | Ligne 39: | ||
The not in method takes three lines (not including the definition of the lists) to find the missing episodes. We can, however, do it better by using sets. | The not in method takes three lines (not including the definition of the lists) to find the missing episodes. We can, however, do it better by using sets. | ||
+ | |||
+ | print(set(List1).difference(set(List2))) | ||
+ | |||
+ | {' | ||
+ | |||
+ | 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))) | print(set(List1).difference(set(List2))) | ||
Ligne 31: | Ligne 65: | ||
{' | {' | ||
- | 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. | ||
print(set(List2).difference(set(List1)) | print(set(List2).difference(set(List1)) | ||
Ligne 43: | Ligne 78: | ||
{' | {' | ||
- | 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.** |
- | You can also use a shortened version of our set difference statement. To do this, we use the - operator. | + | 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. | ||
print(set(List1) - set(List2)) | print(set(List1) - set(List2)) | ||
Ligne 65: | Ligne 115: | ||
We have many other methods available to us that work with sets. They include intersection, | We have many other methods available to us that work with sets. They include intersection, | ||
+ | |||
+ | SetA={1, | ||
+ | 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, | SetA={1, | ||
SetB={5, | SetB={5, | ||
- | Union | + | |
+ | **Union | ||
The union method returns all items from both Set A and Set B. For example: | The union method returns all items from both Set A and Set B. For example: | ||
Ligne 86: | Ligne 161: | ||
You can also use the operator - to perform the difference operation. | You can also use the operator - to perform the difference operation. | ||
+ | |||
+ | 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 | SetA - SetB | ||
- | Intersection | + | |
+ | **Intersection | ||
Intersection returns only the values that are in BOTH SetA and SetB. | Intersection returns only the values that are in BOTH SetA and SetB. | ||
Ligne 112: | Ligne 209: | ||
>>> | >>> | ||
>>> | >>> | ||
+ | |||
+ | 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 | 5 | ||
>>> | >>> | ||
- | If you tried that with a set, the attempt will fail. | + | |
+ | **If you tried that with a set, the attempt will fail. | ||
>>> | >>> | ||
Ligne 147: | Ligne 273: | ||
>>> | >>> | ||
- | 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.** |
- | Frozenset | + | 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 is a class that has many of the same characteristics of a set, but a frozenset is immutable so there are no add or remove methods. | Frozenset is a class that has many of the same characteristics of a set, but a frozenset is immutable so there are no add or remove methods. | ||
Ligne 177: | Ligne 337: | ||
There is a lot more that you can do with sets, but I wanted to cover the basics. I hope that you can see how beneficial sets can be for you. | There is a lot more that you can do with sets, but I wanted to cover the basics. I hope that you can see how beneficial sets can be for you. | ||
- | 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.1635700998.txt.gz · Dernière modification : 2021/10/31 18:23 de auntiee