structures de données (list + dico)

This commit is contained in:
gwen 2017-04-20 14:29:38 +02:00
parent 4e29983800
commit 9cdfc16cc5
2 changed files with 73 additions and 0 deletions

View File

@ -20,6 +20,70 @@ Algorithmes sur les structures de données simples
| Recherche dun mot dans une chaîne de caractères. | On se limite ici à lalgorithme "naïf", en estimant sa complexité. | | Recherche dun mot dans une chaîne de caractères. | On se limite ici à lalgorithme "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 cant add elements to a tuple. Theres no append() or extend() method for tuples,
You cant remove elements from a tuple. Tuples have no remove() or pop() method,
You can find elements in a tuple since this doesnt 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 dont 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 Structures de données complexes
================================ ================================

View File

@ -203,6 +203,15 @@ Il est possible de communiquer de la manière suivante avec un programme :
- lire du texte ou un nombre - lire du texte ou un nombre
- manipuler les prompts - 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) La REPL (boucle d'interaction)
------------------------------- -------------------------------