Erweiterte Datentypen: Container¶

Listen und Tupel¶

In [ ]:
# Listenliterale

values = [5, 10, 20, 30, 50]
print(values)
fractions = [0.5, 0.6, 0.8, 1.0, 1.2]
print(fractions)
names = ["Joe", "Alice", "Bob", "Charly"]
print(names)
heap = [1, "Joe", 1.4, "Alice", 100]
print(heap)
single = ["one"]
leer = []
auch_leer = list()
print(single, leer, auch_leer)
[5, 10, 20, 30, 50]
[0.5, 0.6, 0.8, 1.0, 1.2]
['Joe', 'Alice', 'Bob', 'Charly']
[1, 'Joe', 1.4, 'Alice', 100]
['one'] [] []
In [ ]:
# Tuplelliterale
values = (5, 10, 20, 30, 50)
print(values)
fractions = (0.5, 0.6, 0.8, 1.0, 1.2)
print(fractions)
names = ("Joe", "Alice", "Bob", "Charly")
print(names)
heap = (1, "Joe", 1.4, "Alice", 100)
print(heap)
t_single = ("one",)
t_auch_single = "one",
t_leer = ()
t_auch_leer = tuple()
print(t_single, t_auch_single, t_leer, t_auch_leer)
(5, 10, 20, 30, 50)
(0.5, 0.6, 0.8, 1.0, 1.2)
('Joe', 'Alice', 'Bob', 'Charly')
(1, 'Joe', 1.4, 'Alice', 100)
('one',) ('one',) () ()
In [ ]:
# Umwandlung

l = [1, 2, 3, 4, 5]
t = tuple(l)
print(l, t)
l = [10, 20, 30, 40, 50]
print(l, t)
l2 = list(t)
print(l2)
[1, 2, 3, 4, 5] (1, 2, 3, 4, 5)
[10, 20, 30, 40, 50] (1, 2, 3, 4, 5)
[1, 2, 3, 4, 5]
In [ ]:
s = "hallo"
print(list(s))
print(tuple(s))
['h', 'a', 'l', 'l', 'o']
('h', 'a', 'l', 'l', 'o')

Indizierung¶

In [ ]:
seasons = ["Frühling", "Sommer", "Herbst", "Winter"]
print(seasons[0])   # Erstes Element hat Index 0!
print(seasons[2])
print(seasons[-1])  # Negative Indices zählen vom letzten Element rückwärts
print(seasons[-3])
Frühling
Herbst
Winter
Sommer
In [ ]:
# Ungültige Indizes führen zu einem Fehler (IndexError Exception)
print(seasons[4])  # 4 Elemente => gültige Indizes 0..3
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb Cell 9 line 2
      <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=0'>1</a> # Ungültige Indizes führen zu einem Fehler (IndexError Exception)
----> <a href='vscode-notebook-cell:/home/chris/work/python-kurs-softed/notebooks/containertypes.ipynb#X11sZmlsZQ%3D%3D?line=1'>2</a> print(seasons[4])  # 4 Elemente => gültige Indizes 0..3

IndexError: list index out of range
In [ ]:
# Ebenso
print(seasons[-5])
In [ ]:
# Zuweisung an einzelne Listenelemente

l = [1, 2, 3, 4, 5]
l[1] = -1
print(l)
l[3] = "vier"
print(l)
l[-1] = 5.0
print(l)
In [ ]:
# Indizierung funktioniert auch mit Tupeln mit der selben Syntax
t = (1, 2, 3, 4, 5)
print(t[0], t[-2])
In [ ]:
# Aber Tuple sind nicht-veränderbar (immutable)

t[2] = "drei"

Slicing¶

In [ ]:
l = list(range(10))  # l = [0, 1, ..., 9]
print(l)
print(l[2:4])  # Start ist inklusiv, Ende nicht!
In [ ]:
# Auch wenn kein oder nur ein Element "übrig bleibt", ist das Ergebnis eine Liste

print(l[2:3])
print(l[9:-1])
In [ ]:
# Start und End können leer, also "offen", sein

print(l[5:])
print(l[:3])  # auch hier ist der Ende-Index nicht-inklusiv!
In [ ]:
# Sind Start und Ende leer, ist das Ergebnis eine Kopie der Liste / des Tuples

l1 = [1, 2, 3]
l2 = l1[:]
l3 = l1
l1[0] = 999
print(l1)
print(l2)
print(l3)
In [ ]:
# Zuweisung an Listenslices ist möglich!

seasons = ["Winter", "Frühling", "Sommer", "Herbst"]
seasons[1:1] = ["Karneval"]
print(seasons)
In [ ]:
# Listen oder Tuples können mit + zu einer/m neuen Liste / Tupel zusamengefügt werden 

items = [1, 2, 3]
more_items = items + ["one", "two", "three"]
print(more_items)
items[1:2] = []
print(items)
print(more_items)
more_items += [4, 6, 6]
print(more_items)
In [ ]:
t = (1, 2, 3)
t2 = t + (4, 5, 6)
print(t)
print(t2)
In [ ]:
# Achtung: 

t = (1, 2, 3)
t2 = t
print(t)
print(t2)
print(t is t2)
t += (1, 2, 3)   # Tuples sind immutable, t ist eine neue Kopie!
print(t)
print(t2)
print(t is t2)
s = "ein string"
s += " und noch mehr"
i = 0
i += 1
In [ ]:
l = list(range(10))
import sys
sys.getsizeof(l)

Dictionaries¶

In [ ]:
# Auch Hashtables, Lookup Tables usw. genannt
# Tabellen mit Zurdnung von Schlüsseln zu werten (Key-value pairs)

d = {}
# oder:
d = dict()
print(d)
In [ ]:
scores = {
    "blue team": 40,
    "green team": 45,
    "red team": 30,
    "black team": 60
}
print(scores)
In [ ]:
# Look up

print("The red team scored:", scores["red team"])
In [ ]:
# Neue Key/Value Paare hinzufügen (Dictionaries sind veränderbar!)

scores["white team"] = 45
scores["rainbow team"] = 50
print(scores)
del scores["blue team"]
In [ ]:
# Umwandlung

print(tuple(scores))  # Tupel der Keys

print(list(scores))  # Liste der Keys

print(tuple(scores.keys()))
In [ ]:
# Iteration

for key in scores:
    print("Das", key, "hat", scores[key], "Punkte.")
In [ ]:
# Iteration über Key/Value Paare

print(scores.items())

for key, value in scores.items():
    print("Das", key, "hat", value, "Punkte.")
In [ ]:
# Keys müssen keine Strings, aber immutable sein
# => Integers, Floats, Tuples sind ok

numbers = {1: "one", 2: "two", 3: "three"}
print(numbers[2])  # Achtung: nicht mit Listenindex verwechseln!

pixels = {
    (0, 5): 'r',
    (2, 7): 'b', 
    (3, 10): 'r',
    (5, 2): 'g',
    (5, 5): 'b', 
}

for row in range(6):
    for col in range(10):
        value = pixels.get((row, col), ".")
        print(value, end="")
    print()

Sets (Mengen)¶

Mengen sind wie unsortierte Tupel, in denen jedes Element nur einmal vorkommen kann.

In [ ]:
s = set([])
print(s)   # leeres Set wird repräsentiert mit 'set()', nicht '{}' (leeres Dictionary!)

s2 = set([2,3,5])
s2.add(4)
s2.add(5)  # Schon im Set enthalten
print(s2)

for i in s2:
    print(i)

print()

s2.add(1)
s2.remove(4)

for i in s2:
    print(i)   # Reihenfolge nach Änderung nicht garantiert gleich!
In [ ]:
# Kombination

s1 = set([1, 2, 3, 5])
s2 = set([4, 5, 6, 7])

# Union (alle Elemente aus beiden Sets)
print(s1 | s2)

# Intersection (alle Elemente, die sowohl in s1 als auch s2 sind)
print(s1 & s2)

# Difference (alle Elemente in s1, die nicht auch in s2 sin)
print(s1 - s2)

Siehe: https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset