-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdictionaries.py
More file actions
executable file
·41 lines (29 loc) · 1.02 KB
/
dictionaries.py
File metadata and controls
executable file
·41 lines (29 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# Listentypen:
# 3. Das Dictionary:
# Ein Dictionary ist eine unsortierte Liste, in der
# immer ein <value>/Wert einem <key>/Schlüssel zugeordnet ist.
# Ein Dictionary wird über geschweifte Klammern
# definiert:
dictionary = {"Eins": "one", "Zwei": "two"} # type: dict
dictionary = dict([("Eins", "one"), ("Zwei", "two")])
print(dictionary)
# Auf einen value wird mit Hilfe des keys zu-
# gegriffen:
print(dictionary["Eins"])
# dictionary["nicht da"]: schlägt fehl
dictionary.get("nicht da")
# Ein neues Key-Value-Paar wird erstellt,
# indem auf ein nicht-existierenden value zu-
# gegriffen wird und dieser definiert wird:
dictionary["Wasser"] = "water"
print(dictionary)
# Als keys geeignet sind zum Beispiel: Integer, Strings, Tupel, Boolean
# Einträge löschen
del dictionary["Wasser"]
# Mit len() lässt sich die Länge ausgeben:
print(len(dictionary))
# Die Schlüssel eines Dictionarys können als Liste zurückgegeben werden:
print(dictionary.keys())
# ebenso wie die Values:
print(dictionary.items())