778 lines
17 KiB
Plaintext
778 lines
17 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Funktionsparameter benennen\n",
|
||
|
"Funktionen enthalten Parameter mit denen Sie arbeiten können/müssen. Diese Parameter können vorgegeben werden oder jedes mal übergeben. Mit Standardparametern kann man vorgegebene Parameter übergeben, diese aber auch einfach durch andere Parameter abändern."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Hallo\n",
|
||
|
"Hallo\n",
|
||
|
"Welt\n",
|
||
|
"Welt\n",
|
||
|
"Welt\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def multi_print(nummer = 1, word = \"Hallo\"):\n",
|
||
|
" for i in range(0, nummer):\n",
|
||
|
" print(word)\n",
|
||
|
" \n",
|
||
|
"multi_print(2) # Parameter 'Nummer' wird auf 5 gesetzt. 'Hallo' wird beibehalten\n",
|
||
|
"multi_print(word = \"Welt\", nummer = 3) # Parameter 'Welt' und '5' werden übergeben"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Daten Sortieren\n",
|
||
|
"Daten können über Python-integrierte Funktionen sortiert werden. Nützlich ist die **sort**-Methode.<br>\n",
|
||
|
".sort() sortiert nach dem Alphabet"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['Erik', 'Franziska', 'Max', 'Monika']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l = [\"Max\", \"Monika\", \"Erik\", \"Franziska\"]\n",
|
||
|
"l.sort()\n",
|
||
|
"print(l)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
".sort können auch einige Paramter zum sortieren übergeben werden: reverse (umgekehrt), Schlüssel (wonach sortieren), ..."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['Monika', 'Max', 'Franziska', 'Erik']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l = [\"Max\", \"Monika\", \"Erik\", \"Franziska\"]\n",
|
||
|
"l.sort(reverse=True)\n",
|
||
|
"print(l)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['Max', 'Erik', 'Monika', 'Franziska']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l = [\"Max\", \"Monika\", \"Erik\", \"Franziska\"]\n",
|
||
|
"l.sort(key=len)\n",
|
||
|
"print(l)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Achtung! .sorted() gibt eine Liste zurück; auch wenn ein Dictionarie sortiert werden soll."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['Saigon', 'Köln', 'Budapest']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"d = {\"Köln\": \"CGN\", \"Budapest\": \"BUD\", \"Saigon\": \"SGN\"}\n",
|
||
|
"\n",
|
||
|
"print(sorted(d, reverse = True))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"mit sorted() wird eine neue Liste mit den Parameter erstellt, ohne die bestehende Liste zu überschreiben"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"['Erik', 'Franziska', 'Max', 'Monika']\n",
|
||
|
"['Max', 'Monika', 'Erik', 'Franziska']\n",
|
||
|
"['Erik', 'Franziska', 'Max', 'Monika']\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"l = [\"Max\", \"Monika\", \"Erik\", \"Franziska\"]\n",
|
||
|
"print( sorted(l) )\n",
|
||
|
"print(l)\n",
|
||
|
"l.sort()\n",
|
||
|
"print(l)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Lambda Funktion\n",
|
||
|
"kurze Schreibweise um eine Funktion als Parameter übergeben"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"[('Franziska', 1), ('Monika', 2), ('Max', 3), ('Erik', 3)]\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"students = [\n",
|
||
|
" (\"Max\", 3),\n",
|
||
|
" (\"Monika\", 2),\n",
|
||
|
" (\"Erik\", 3),\n",
|
||
|
" (\"Franziska\", 1)\n",
|
||
|
"]\n",
|
||
|
"\n",
|
||
|
"students.sort(key=lambda student: student[1])\n",
|
||
|
"print(students)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"1"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 15,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"f = lambda student: student[1]\n",
|
||
|
"f((\"Max\", 1))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Durchsuchen mit Regulären Ausdrücken \n",
|
||
|
"Reguläre Ausdrücke erlauben, Strings flexibel durchsuchen zu können. Mehr infos unter [RegExr](https://regexr.com/)."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 29,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"['13', '24', '8']"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 29,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import re\n",
|
||
|
"sentence = \"Meine 13 Katzen benötigen 24 Kilo Katzenfutterutter und 8 Liter Wasser undsoweiter.\"\n",
|
||
|
"re.findall(\"[0-9]+\", sentence) "
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 30,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"<_sre.SRE_Match object; span=(6, 8), match='13'>"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 30,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"re.search(\"[0-9]+\", sentence)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 31,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"<_sre.SRE_Match object; span=(6, 8), match='13'>\n",
|
||
|
"<_sre.SRE_Match object; span=(6, 8), match='13'>\n",
|
||
|
"<_sre.SRE_Match object; span=(9, 15), match='Katzen'>\n",
|
||
|
"<_sre.SRE_Match object; span=(9, 14), match='Katze'>\n",
|
||
|
"<_sre.SRE_Match object; span=(52, 55), match='und'>\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print( re.search(\"[0123456789]+\", sentence)) # lange Form\n",
|
||
|
"print( re.search(\"[0-9]+\", sentence)) # kurze Form aller Zahlen\n",
|
||
|
"print( re.search(\"Katzen+\", sentence))\n",
|
||
|
"print( re.search(\"Katze?\", sentence))\n",
|
||
|
"print( re.search(\"und*\", sentence))\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Datum\n",
|
||
|
"Zur Darstellung von Datumswerten gibt es in Python das datetime-Modul (https://docs.python.org/3/library/datetime.html). Damit können Datumswerte repräsentiert und damit gerechnen werden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2019-03-16 10:58:09.805770\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from datetime import datetime\n",
|
||
|
"now = datetime.now() # Über datetime.now() ein Datumsobjekt zum aktuellen Datum erstellen\n",
|
||
|
"print(now)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2019-12-11 10:09:08\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"#über ein spezifisches Datum (hier: 20.8.2017, 20:00:00):\n",
|
||
|
"day = datetime(2019, 12, 11, 10, 9, 8)\n",
|
||
|
"print(day)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Aud die Datumsobjekte kann über Selektoren auf einzelne Angaben direkt Zugegriffen werden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Jahr = 2019\n",
|
||
|
"Monat = 12\n",
|
||
|
"Tag = 11\n",
|
||
|
"Stunde = 10\n",
|
||
|
"Minute = 9\n",
|
||
|
"Sekunde = 8\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(\"Jahr = \" + str(day.year))\n",
|
||
|
"print(\"Monat = \" + str(day.month))\n",
|
||
|
"print(\"Tag = \" + str(day.day))\n",
|
||
|
"print(\"Stunde = \" + str(day.hour))\n",
|
||
|
"print(\"Minute = \" + str(day.minute))\n",
|
||
|
"print(\"Sekunde = \" + str(day.second))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Die `.timestamp()`-Methode gibt den entsprechenden Unix-Timestamp zu einem bestimmten Datumswert zurück. Unix-Timestamp ist eine Zahl, die die Sekunden seit dem 01.01.1970 hochzählt.\n",
|
||
|
"\n",
|
||
|
"Vorteil bei einem Unix-Timestamp ist, dass dieser kompakt gespeichert werden kann. Intern muss der Computer dadurch nur eine Zahl speichern, um einen Datumswert zu repräsentieren."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"23325058.19423008\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(day.timestamp() - now.timestamp()) # Zeitunterschied in Sekunden"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### `date`- und `time`- Angaben\n",
|
||
|
"Das `datetime`-Paket stellt weitere Klassen zur Verfügung, die für Datumsangaben verwendet werden können.\n",
|
||
|
"- `datetime`: Datumsangabe + Zeitangabe\n",
|
||
|
"- `date`: Nur Datumsangabe\n",
|
||
|
"- `time`: Nur Zeitangabe"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from datetime import date, time"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2017-08-20\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"d = date(2017, 8, 20)\n",
|
||
|
"print(d)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 18,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"20:01:04\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"t = time(20, 1, 4)\n",
|
||
|
"print(t)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"Datumswerte können auch vergleicht werden.\n",
|
||
|
"Beachte, wie bei den Ojekten die Datumsangabe als auch die Zeitangabe übereinstimmen müssen\n",
|
||
|
"\n",
|
||
|
"- `datetime`: Datumsangabe + Zeitangabe müssen übereinstimmen\n",
|
||
|
"- `date`: Datumsangabe muss übereinstimmen\n",
|
||
|
"- `time`: Zeitangabe muss übereinstimmen"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 20,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"True\n",
|
||
|
"False\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(date(2018, 7, 6) == date(2018, 7, 6))\n",
|
||
|
"print(datetime(2018, 12, 11, 10, 9, 8) == datetime(2018, 12, 11, 10, 9, 0))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### `datetime` in `date` und `time` umwandeln\n",
|
||
|
"`datetime`-Objekt können in ein `date` und ein `time`-Objekt zerlegt werden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 21,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"20:00:00\n",
|
||
|
"2017-08-20\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"dt = datetime(2017, 8, 20, 20, 0, 0)\n",
|
||
|
"print(dt.time())\n",
|
||
|
"print(dt.date())"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### `date` und `time` in `datetime` umwandeln\n",
|
||
|
"`datetime`-Objekt können aus einem `date` und einem `time`-Objekt zusammengesetzt werden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2017-08-20 20:30:00\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(datetime.combine(date(2017, 8, 20), time(20, 30, 0)))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Datumswerte ausgeben\n",
|
||
|
"Dokumentation: https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior\n",
|
||
|
"Es ist wichtig, Datumswerte auch richtig auszugeben."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2019-03-16 11:08:41.295840\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from datetime import datetime\n",
|
||
|
"now = datetime.now()\n",
|
||
|
"print(now)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 24,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"16.03.2019\n",
|
||
|
"2019-03-16\n",
|
||
|
"20190316\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(now.strftime(\"%d.%m.%Y\"))\n",
|
||
|
"print(now.strftime(\"%Y-%m-%d\"))\n",
|
||
|
"print(now.strftime(\"%Y%m%d\"))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"### Datumswerte einlesen\n",
|
||
|
"Datumswerte können auch aus einem String extrahiert werden."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2017-07-18 00:00:00\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"d = \"18.07.2017\"\n",
|
||
|
"print(datetime.strptime(d, \"%d.%m.%Y\"))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Zeitdifferenzen: Rechnen mit Datumswerten"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 26,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2019-03-16 11:09:45.018845\n",
|
||
|
"2019-04-05 15:12:46.018845\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"from datetime import datetime, timedelta\n",
|
||
|
"now = datetime.now()\n",
|
||
|
"print(now)\n",
|
||
|
"print(now + timedelta(days = 20, hours = 4, minutes = 3, seconds = 1))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 29,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"-1 day, 12:50:14.981155\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"day = datetime(2019, 2, 16)\n",
|
||
|
"td = day - now\n",
|
||
|
"print(td)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 30,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"2018-12-31 12:50:14.981155\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"print(datetime(2019, 1, 1) + td)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Das DefaultDict-Modul\n",
|
||
|
"\n",
|
||
|
"Das DefaultDict-Modul stellt eine ganz besondere Funktionalität zur Verfügung: Du kannst damit ein Dictionary erstellen, welches sich mehr oder weniger automatisch mit initialen Werten befüllt!\n",
|
||
|
"\n",
|
||
|
"Das möchten wir uns natürlich mal anschauen:"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"from collections import defaultdict"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"generate() wurde aufgerufen!\n",
|
||
|
"defaultdict(<function generate at 0x000001757CEF7EA0>, {'existiertNicht': 5})\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"def generate():\n",
|
||
|
" print(\"generate() wurde aufgerufen!\")\n",
|
||
|
" return 0\n",
|
||
|
"\n",
|
||
|
"d = defaultdict(generate)\n",
|
||
|
"\n",
|
||
|
"d[\"existiertNicht\"] = d[\"existiertNicht\"] + 5\n",
|
||
|
"print(d)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"defaultdict(<class 'int'>, {'Hallo': 2, 'Welt': 1})\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"p = defaultdict(int)\n",
|
||
|
"words = [\"Hallo\", \"Hallo\", \"Welt\"]\n",
|
||
|
"\n",
|
||
|
"for word in words: # für jedes neue Wort wird defaultdict aufgerufen und ein eintrag erstellt\n",
|
||
|
" p[word] = p[word] + 1 # für jedes wiederholte Wort wird der Wert um 1 erhöht\n",
|
||
|
"\n",
|
||
|
"print(p)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": []
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.6.8"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 2
|
||
|
}
|