Outils pour utilisateurs

Outils du site


issue76:python

Ceci est une ancienne révision du document !


Table des matières

1

Usually, my articles are fairly long. However, due to some medical issues, this will be a fairly short article (in the grand scheme of things) this month. However, we will push through and continue our series on the media manager program. One of the things our program will do for us is let us know if we have any missing episodes from any given series in the database. Here's the scenario. We have a series, we'll call it “That 80's Show”, that ran for three seasons. In season 2, there were 15 episodes. However, we have only 13 of them in our library. How do we find which episodes are missing – programmatically? 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), here is the definition of a set:

Habituellement, mes articles sont assez longs. Toutefois, en raison de certains problèmes médicaux, celui de ce mois-ci sera assez court (dans le grand schéma des choses). Cependant, nous allons pouvoir continuer et approfondir notre série sur le programme de gestion de données.

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 ?

Le plus simple est d'utiliser les listes et les ensembles. Nous avons déjà utilisé des listes dans un certain nombre d'articles au cours des quatre dernières années, mais les ensembles sont un nouveau type de données dans cette série, nous allons donc les examiner un peu. Selon la « documentation officielle » pour Python (docs.python.org), voici la définition d'un ensemble :

2

“A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.” I'll continue to use the example from the documentation page to illustrate the process. »> Basket = ['apple','orange','apple','pear','orange','banana'] »> fruit = set(basket) »> fruit set(['orange','pear','apple','banana'])

« Un ensemble est une collection sans notion d'ordre et sans doublon. Les utilisations de base comprennent des tests d'appartenance et l'élimination des doublons. On peut également faire des opérations mathématiques sur les ensembles comme l'union, l'intersection, la différence et la différence symétrique ».

Je vais continuer à utiliser l'exemple de la page de documentation pour illustrer le processus.

panier = ['pomme', 'orange', 'pomme', 'poire', 'orange', 'banane'] fruit = ensemble(panier) fruit ensemble(['orange', 'poire', 'pomme', 'banane'])

3

Notice that in the original list that was assigned to the basket variable, apple and orange were put in twice, but, when we assigned it to a set, the duplicates were discarded. Now, to use the set that we just created, we can check to see if an item of fruit (or something else) is in the set. We can use the “in” operator.

'orange' in fruit

True

'kiwi' in fruit

False

4

That's pretty simple and, hopefully, you are beginning to see where all this is going. Let's say we have a shopping list that has a bunch of fruit in it, and, as we go through the store, we want to check what we are missing – basically the items in the shopping list but not in our basket. We can start like this.

shoppinglist = ['orange','apple','pear','banana','kiwi','grapes']
basket = ['apple','kiwi','banana']
sl = set(shoppinglist)
b = set(basket)
sl-b

set(['orange', 'pear', 'grapes'])

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.

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.

6

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.

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.

issue76/python.1391117082.txt.gz · Dernière modification : 2014/01/30 22:24 de fredphil91