Diferències
Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Següent revisió | Revisió prèvia | ||
info:cursos:netacad:python:pe1m4:diccionarios [21/06/2022 10:25] – creat mate | info:cursos:netacad:python:pe1m4:diccionarios [08/09/2022 03:03] (actual) – [Puntos Clave: Diccionarios] mate | ||
---|---|---|---|
Línia 339: | Línia 339: | ||
</ | </ | ||
+ | === Puntos Clave: Diccionarios | ||
+ | 1. Los diccionarios son *colecciones indexadas de datos, mutables y desordenadas. (*En Python 3.6x los diccionarios están ordenados de manera predeterminada. | ||
+ | |||
+ | Cada diccionario es un par de clave : valor. Se puede crear empleado la siguiente sintaxis: | ||
+ | <code python> | ||
+ | my_dictionary = { | ||
+ | key1: value1, | ||
+ | key2: value2, | ||
+ | key3: value3, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 2. Si se desea acceder a un elemento del diccionario, | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | item_1 = pol_esp_dictionary[" | ||
+ | print(item_1) | ||
+ | |||
+ | item_2 = pol_esp_dictionary.get(" | ||
+ | print(item_2) | ||
+ | </ | ||
+ | |||
+ | 3. Si se desea cambiar el valor asociado a una clave específica, | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | pol_esp_dictionary[" | ||
+ | item = pol_esp_dictionary[" | ||
+ | print(item) | ||
+ | </ | ||
+ | |||
+ | 4. Para agregar o eliminar una clave (junto con su valor asociado), emplea la siguiente sintaxis: | ||
+ | <code python> | ||
+ | phonebook = {} # un diccionario vacío | ||
+ | |||
+ | phonebook[" | ||
+ | print(phonebook) | ||
+ | |||
+ | del phonebook[" | ||
+ | print(phonebook) | ||
+ | </ | ||
+ | |||
+ | Además, se puede insertar un elemento a un diccionario utilizando el método '' | ||
+ | <code python> | ||
+ | pol_esp_dictionary = {" | ||
+ | |||
+ | pol_esp_dictionary.update({" | ||
+ | print(pol_esp_dictionary) | ||
+ | |||
+ | pol_esp_dictionary.popitem() | ||
+ | print(pol_esp_dictionary) | ||
+ | </ | ||
+ | |||
+ | 5. Se puede emplear el bucle '' | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | for item in pol_esp_dictionary: | ||
+ | print(item) | ||
+ | |||
+ | # salida: zamek | ||
+ | # woda | ||
+ | # gleba | ||
+ | </ | ||
+ | 6. Si deseas examinar los elementos (claves y valores) del diccionario, | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | for key, value in pol_esp_dictionary.items(): | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | 7. Para comprobar si una clave existe en un diccionario, | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | if " | ||
+ | print(" | ||
+ | else: | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | 8. Se puede emplear la palabra reservada '' | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | print(len(pol_esp_dictionary)) | ||
+ | del pol_esp_dictionary[" | ||
+ | print(len(pol_esp_dictionary)) | ||
+ | |||
+ | pol_esp_dictionary.clear() | ||
+ | print(len(pol_esp_dictionary)) | ||
+ | |||
+ | del pol_esp_dictionary | ||
+ | </ | ||
+ | |||
+ | 9. Para copiar un diccionario, | ||
+ | <code python> | ||
+ | pol_esp_dictionary = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | |||
+ | copy_dictionary = pol_esp_dictionary.copy() | ||
+ | </ |