issue76: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 | ||
issue76:python [2014/01/30 22:30] – [4] fredphil91 | issue76:python [2014/02/01 19:07] (Version actuelle) – [6] auntiee | ||
---|---|---|---|
Ligne 7: | Ligne 7: | ||
The simplest way is to use lists and sets. We have already used lists in a number of the articles over the last four years, but Sets are a new data type to this series, so we'll examine them for a while. According to the “official documentation” for Python (docs.python.org), | The simplest way is to use lists and sets. We have already used lists in a number of the articles over the last four years, but Sets are a new data type to this series, so we'll examine them for a while. According to the “official documentation” for Python (docs.python.org), | ||
- | Habituellement, | + | Habituellement, |
- | Une des choses que notre programme fera pour nous est de nous avertir si nous avons des épisodes manquants dans une des séries de la base de données. Voici le scénario. Nous avons une série, appelons-là « La série des années 80 », qui a duré trois saisons. Dans la saison 2, il y avait 15 épisodes. Cependant, nous n'en avons que 13 dans notre bibliothèque. Comment trouver quels épisodes manquent - informatiquement ? | + | Une des choses que notre programme fera pour nous est de nous avertir si nous avons des épisodes manquants dans une des séries de la base de données. Voici le scénario. Nous avons une série, appelons-la « La série des années 80 », qui a duré trois saisons. Dans la saison 2, il y avait 15 épisodes. Cependant, nous n'en avons que 13 dans notre bibliothèque. Comment trouver quels épisodes manquent - informatiquement ? |
- | Le plus simple est d' | + | Le plus simple est d' |
====== 2 ====== | ====== 2 ====== | ||
Ligne 27: | Ligne 27: | ||
set([' | set([' | ||
- | « Un ensemble est une collection sans notion d' | + | « Un ensemble est une collection sans notion d' |
Je vais continuer à utiliser l' | Je vais continuer à utiliser l' | ||
Ligne 80: | Ligne 80: | ||
>>> | >>> | ||
- | C'est assez simple et, je l' | + | C'est assez simple et, je l' |
>>> | >>> | ||
Ligne 98: | Ligne 98: | ||
====== 5 ====== | ====== 5 ====== | ||
- | We create our two lists, shoppinglist for what we need and basket for what we have. We assign each to a set and then use the set difference operator (the minus sign) to give us the items that are in the shopping list but not in the basket. | + | **We create our two lists, shoppinglist for what we need and basket for what we have. We assign each to a set and then use the set difference operator (the minus sign) to give us the items that are in the shopping list but not in the basket. |
Now, using the same logic, we will create a routine (next page, bottom left) that will deal with our missing episodes. We will call our routine “FindMissing” and pass it two variables. The first is an integer that is set to the number of episodes in that season and the second is a list containing the episode numbers that we have for that season. | Now, using the same logic, we will create a routine (next page, bottom left) that will deal with our missing episodes. We will call our routine “FindMissing” and pass it two variables. The first is an integer that is set to the number of episodes in that season and the second is a list containing the episode numbers that we have for that season. | ||
- | The routine, when you run it, prints out [5, 8, 15], which is correct. Now let's look at the code. The first line creates a set called EpisodesNeeded using a list of integers created using the range function. We need to give the range function the start value and end value. We add 1 to the range high value to give us the correct list of values from 1 to 15. Remember the range function is actually 0 based, so when we give it 16 (expected (15) + 1), the actual list that range creates is 0 to 15. We tell the range function to start at 1, so even though the range is 0 to 15 which is 16 values, we want 15 starting at 1. | + | The routine, when you run it, prints out [5, 8, 15], which is correct. Now let's look at the code. The first line creates a set called EpisodesNeeded using a list of integers created using the range function. We need to give the range function the start value and end value. We add 1 to the range high value to give us the correct list of values from 1 to 15. Remember the range function is actually 0 based, so when we give it 16 (expected (15) + 1), the actual list that range creates is 0 to 15. We tell the range function to start at 1, so even though the range is 0 to 15 which is 16 values, we want 15 starting at 1.** |
+ | |||
+ | Nous créons nos deux listes, listecourses pour ce que nous voulons et panier pour ce que nous avons. Nous affectons chacune à un ensemble et utilisons l' | ||
+ | |||
+ | Maintenant, en utilisant la même logique, nous allons créer une routine (page suivante, en bas à gauche) qui traitera de nos épisodes manquants. Nous allons appeler notre routine « RechercherManquants » et lui passer deux variables. La première est un entier réglé au nombre d' | ||
+ | |||
+ | La routine, lorsque vous l' | ||
====== 6 ====== | ====== 6 ====== | ||
- | Next we create a set from the list that is passed into our routine, which contains the episode numbers that we actually have. | + | **Next we create a set from the list that is passed into our routine, which contains the episode numbers that we actually have. |
Now we can create a list using the set difference operator on the two sets. We do this so we can sort it with the list.sort() method. You can certainly return the list if you wish, but in this iteration of the routine, we’ll just print it out. | Now we can create a list using the set difference operator on the two sets. We do this so we can sort it with the list.sort() method. You can certainly return the list if you wish, but in this iteration of the routine, we’ll just print it out. | ||
Ligne 112: | Ligne 118: | ||
Well, that’s all the time in the chair in front of the computer that my body can stand, so I’ll leave you for this month, wondering how we are going to use this in our media manager. | Well, that’s all the time in the chair in front of the computer that my body can stand, so I’ll leave you for this month, wondering how we are going to use this in our media manager. | ||
- | Have a good month and see you soon. | + | Have a good month and see you soon.** |
+ | |||
+ | Ensuite, nous créons un ensemble avec la liste qui est passée à notre routine, qui contient les numéros d' | ||
+ | |||
+ | Maintenant, nous pouvons créer une liste en utilisant l' | ||
+ | |||
+ | Eh bien, me voici au bout du temps que mon corps peut supporter assis dans le fauteuil en face de l' | ||
+ | |||
+ | Passez un bon mois et à bientôt. | ||
+ | |||
+ | ====== 7 (code page 10) ====== | ||
+ | |||
+ | def RechercherManquants(attendus, | ||
+ | # | ||
+ | # attendus : numeros de tous les episodes requis | ||
+ | # dejapresents : liste des episodes que nous avons deja | ||
+ | # renvoie une liste triee des numeros manquants | ||
+ | # | ||
+ | EpisodesRequis = set(range(1, | ||
+ | EpisodesPresents = set(dejapresents) | ||
+ | EncoreBesoin = list(EpisodesRequis - EpisodesPresents) | ||
+ | EncoreBesoin.sort() | ||
+ | print EncoreBesoin | ||
+ | RechercherManquants(15, | ||
issue76/python.1391117439.txt.gz · Dernière modification : 2014/01/30 22:30 de fredphil91