From b8bf37cafb76831fe136cad1f01d177b5a1e7269 Mon Sep 17 00:00:00 2001 From: gwen Date: Thu, 20 Apr 2017 14:29:38 +0200 Subject: [PATCH] =?UTF-8?q?structures=20de=20donn=C3=A9es=20(list=20+=20di?= =?UTF-8?q?co)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- algorithmique/cours/donnees.txt | 64 +++++++++++++++++++++++++++++++ algorithmique/cours/programme.txt | 9 +++++ 2 files changed, 73 insertions(+) diff --git a/algorithmique/cours/donnees.txt b/algorithmique/cours/donnees.txt index aad702a..b34b259 100644 --- a/algorithmique/cours/donnees.txt +++ b/algorithmique/cours/donnees.txt @@ -20,6 +20,70 @@ Algorithmes sur les structures de données simples | Recherche d’un mot dans une chaîne de caractères. | On se limite ici à l’algorithme "naïf", en estimant sa complexité. | +------------------------------------------------------+------------------------------------------------------------------------+ +Liste +------ + +.. code-block:: python + + zoo = ['bear', 'lion', 'panda', 'zebra'] + print(zoo) + + # But these list elements are not + biggerZoo = ['bear', 'lion', 'panda', 'zebra', ['chimpanzees', 'gorillas', 'orangutans', 'gibbons']] + print(biggerZoo) + +FIXME + +- Lists Versus Tuples +- Lists Versus Sets + + +Tuples are used to collect an immutable ordered list of elements. This means that: + + You can’t add elements to a tuple. There’s no append() or extend() method for tuples, + You can’t remove elements from a tuple. Tuples have no remove() or pop() method, + You can find elements in a tuple since this doesn’t change the tuple. + You can also use the in operator to check if an element exists in the tuple. + + + A list stores an ordered collection of items, so it keeps some order. Dictionaries don’t have any order. + Dictionaries are known to associate each key with a value, while lists just contain values. + +Use a dictionary when you have an unordered set of unique keys that map to values. + + +.. code-block:: python + + >>> listOfStrings = ['One', 'Two', 'Three'] + >>> strOfStrings = ' '.join(listOfStrings) + >>> print(strOfStrings) + One Two Three + >>> + >>> # List Of Integers to a String + ... listOfNumbers = [1, 2, 3] + >>> strOfNumbers = ''.join(str(n) for n in listOfNumbers) + >>> print(strOfNumbers) + 123 + >>> + + + +Modification de la structure de données +---------------------------------------- + +.. code-block:: python + + >>> l = [('host1', '10.1.2.3', '6E:FF:56:A2:AF:18'), ('host2', '10.1.2.4', '6E:FF:56:A2:AF:17'), ('host3', '10.1.2.5', '6E:FF:56:A2:AF:19')] + >>> result = [] + >>> for hostname, ip, macaddress in l: + ... result.append(dict(hostname=hostname, ip=ip, macaddress=macaddress)) + ... + >>> result + [{'hostname': 'host1', 'ip': '10.1.2.3', 'macaddress': '6E:FF:56:A2:AF:18'}, + {'hostname': 'host2', 'ip': '10.1.2.4', 'macaddress': '6E:FF:56:A2:AF:17'}, + {'hostname': 'host3', 'ip': '10.1.2.5', 'macaddress': '6E:FF:56:A2:AF:19'}] + >>> + Structures de données complexes ================================ diff --git a/algorithmique/cours/programme.txt b/algorithmique/cours/programme.txt index 0c51137..0a528d6 100644 --- a/algorithmique/cours/programme.txt +++ b/algorithmique/cours/programme.txt @@ -203,6 +203,15 @@ Il est possible de communiquer de la manière suivante avec un programme : - lire du texte ou un nombre - manipuler les prompts +Exemple de lecture d'une entrée utilisateur + +.. block-code:: python + + # coding: utf-8 + prenom = raw_input("quel est ton prénom ? \n") + print("bonjour, je m'appelle " + prenom.capitalize()) + + La REPL (boucle d'interaction) -------------------------------