structures de données (list + dico)
This commit is contained in:
parent
4e29983800
commit
9cdfc16cc5
@ -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
|
||||
================================
|
||||
|
@ -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)
|
||||
-------------------------------
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user