This commit is contained in:
2019-02-21 20:33:25 +01:00
parent b1971ee4e0
commit 2cac531bf0
3 changed files with 20735 additions and 0 deletions

75
2/DataLabo1_01.txt Normal file
View File

@@ -0,0 +1,75 @@
Albert Einstein 03/14/1879
Benjamin Franklin 01/17/1706
J. Edgar Hoover 01/01/1895
Mel Gibson 01/03/1956
Michael Schumacher 01/03/1969
Richard Nixon 01/09/1913
Orlando Bloom 01/13/1977
Kate Moss 01/16/197
Betty White 01/17/1922
Mohammed Ali 01/17/1942
Bridget Fonda 01/27/1964
Phill Collins 01/30/1951
John Ford 02/01/1895
Brandon Lee 02/01/1965
Charles Lindbergh 02/04/1902
Alice Cooper 02/04/1948
Ronald Reagon 02/06/1911
Axl Rose 02/06/1962
Charles Dickens 02/07/1812
Robert Wagner 02/10/1930
Jennifer Anniston 02/11/1969
Nina Simone 02/21/1933
Steven Jobs 02/24/1955
Johnny Cash 02/26/1932
Elisabeth Taylor 02/27/1932
Bobby Fisher 03/09/1943
Osama Bin Laden 03/10/1957
Aretha Franklin 03/25/1942
Marlon Brando 04/03/1924
Bette Davies 04/05/1908
Russel Crowe 04/07/1964
Iggy Pop 04/21/1947
Al Pacino 04/25/1940
James Brown 05/03/1933
Dr Benjamin Spock 05/02/1903
Karl Marx 05/05/1818
Sigmund Freud 05/06/1856
George Lukas 05/14/1944
Madeleine Albright 05/15/1937
Naomi Campbell 05/22/1970
Bob Dylan 05/24/1941
John F. Kennedy 05/29/1917
Prinses Diana 06/01/1961
Tom Cruise 06/03/1962
Dalai Lama 06/06/1935
O.J. Simpson 06/09/1947
William Clark 08/01/1770
Yves Saint Laurent 08/01/1936
Louis Armstrong 08/04/1901
Steve Martin 08/14/1945
Ben Affleck 08/15/1972
Patrick Swayze 08/18/1952
Roman Polanski 08/18/1933
Tori Amos 08/22/1963
Moeder Teresa 08/27/1910
Freddy Mercury 09/05/1946
Hugh Grant 09/09/1960
Prins Harry 09/15/1984
Oliver Stone 09/15/1946
Leonard Cohen 09/21/1934
Stephen King 09/21/1947
Bruce Springsteen 09/23/1949
Julie Andrews 10/01/1935
Bob Geldof 10/05/1954
Kate Winslet 10/05/1975
Cliff Richard 10/14/1940
Oscar Wilde 10/16/1854
Alfred Nobel 10/21/1833
Dolph Lungren 11/03/1959
Tara Reid 11/08/1975
Demi Moore 11/11/1962
Leonardo di Caprio 11/11/1974
Condoleezza Rice 11/14/1954
Prins charles 11/14/1948
Isaac Newton 12/25/1642

19850
2/DataLabo1_02.txt Normal file

File diff suppressed because it is too large Load Diff

810
2/Labo2.ipynb Normal file
View File

@@ -0,0 +1,810 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Labo 2 Data Science : Data structures in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dictionnaries "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 1$ : Vervolledig eerst oef5, oef6 en oef7 van Labo1. \n",
"\n",
"_Tip_ : Onderstaand stukje code toont hoe je simpel de lijnen uit een tekstbestand kan omzetten naar een lijst in Python waarvan elk element opnieuw een lijst is. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path1 = 'C:/Users/katja.verbeeck/Documents/Python Scripts/DataLabo_01.txt'\n",
"with open(path1) as f:\n",
" data = [x for x in f]\n",
"print(data)\n",
"data = [x.rstrip() for x in data]\n",
"print(data)\n",
"data = [d.split(' ') for d in data]\n",
"print(data)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tuples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tuples in Python zijn bedoeld om heterogene data te groeperen. In andere talen kan je dit vergelijken met het concept van records of zelfs structs. Syntactisch gezien, gaat het om een sequentie van waarden gescheiden door een komma. Wat bewerkingen betreft kan je ze vergelijken met Strings: indexering en slicing kan, maar ze zijn eveneens onveranderbaar. Hieronder nog enkele leuke eigenschappen van tuples:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Packing en unpacking\n",
"student = (\"Bob\", 19,\"Computer Science\")\n",
"name,age,studies = student\n",
"print(name,age,studies)\n",
"\n",
"#swapping\n",
"print(name,age)\n",
"name,age = age,name\n",
"print(name,age)\n",
"\n",
"#het aantal vars en waarden moet wel kloppen natuurlijk\n",
"#(a,b,c,d) = (1,2,3)\n",
"\n",
"#Tuples als return waarden : je kan meerdere resultaten teruggeven\n",
"from math import pi\n",
"def omtrek_opp(straal):\n",
" omtrek = round(2 * pi * straal * 100) / 100\n",
" opp = round(pi * straal**2 * 100) / 100\n",
" return (omtrek,opp)\n",
"\n",
"omtrek_opp(5)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 2$ : Werk opnieuw met de txt file van verjaardagen : $DataLabo\\_01.txt$. Lees deze nu in als een lijst van tuples als volgt : elk tuple bevat als eerste element de naam van de persoon, het tweede element is zijn geboortedatum maar nu omgevormd tot een $date$ object :\n",
" ('Bruce Springsteen' datetime.date(1949,9,23))\n",
" \n",
"_Let op_ : de .txt file bevat ergens een type-fout in een datum, de bedoeling is dat je code deze foutieve data kan negeren"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Albert Einstein', datetime.date(1879, 3, 14)), ('Benjamin Franklin', datetime.date(1706, 1, 17)), ('J. Edgar Hoover', datetime.date(1895, 1, 1)), ('Mel Gibson', datetime.date(1956, 1, 3)), ('Michael Schumacher', datetime.date(1969, 1, 3)), ('Richard Nixon', datetime.date(1913, 1, 9)), ('Orlando Bloom', datetime.date(1977, 1, 13)), ('Kate Moss', datetime.date(197, 1, 16)), ('Betty White', datetime.date(1922, 1, 17)), ('Mohammed Ali', datetime.date(1942, 1, 17)), ('Bridget Fonda', datetime.date(1964, 1, 27)), ('Phill Collins', datetime.date(1951, 1, 30)), ('John Ford', datetime.date(1895, 2, 1)), ('Brandon Lee', datetime.date(1965, 2, 1)), ('Charles Lindbergh', datetime.date(1902, 2, 4)), ('Alice Cooper', datetime.date(1948, 2, 4)), ('Ronald Reagon', datetime.date(1911, 2, 6)), ('Axl Rose', datetime.date(1962, 2, 6)), ('Charles Dickens', datetime.date(1812, 2, 7)), ('Robert Wagner', datetime.date(1930, 2, 10)), ('Jennifer Anniston', datetime.date(1969, 2, 11)), ('Nina Simone', datetime.date(1933, 2, 21)), ('Steven Jobs', datetime.date(1955, 2, 24)), ('Johnny Cash', datetime.date(1932, 2, 26)), ('Elisabeth Taylor', datetime.date(1932, 2, 27)), ('Bobby Fisher', datetime.date(1943, 3, 9)), ('Osama Bin Laden', datetime.date(1957, 3, 10)), ('Aretha Franklin', datetime.date(1942, 3, 25)), ('Marlon Brando', datetime.date(1924, 4, 3)), ('Bette Davies', datetime.date(1908, 4, 5)), ('Russel Crowe', datetime.date(1964, 4, 7)), ('Iggy Pop', datetime.date(1947, 4, 21)), ('Al Pacino', datetime.date(1940, 4, 25)), ('James Brown', datetime.date(1933, 5, 3)), ('Dr Benjamin Spock', datetime.date(1903, 5, 2)), ('Karl Marx', datetime.date(1818, 5, 5)), ('Sigmund Freud', datetime.date(1856, 5, 6)), ('George Lukas', datetime.date(1944, 5, 14)), ('Madeleine Albright', datetime.date(1937, 5, 15)), ('Naomi Campbell', datetime.date(1970, 5, 22)), ('Bob Dylan', datetime.date(1941, 5, 24)), ('John F. Kennedy', datetime.date(1917, 5, 29)), ('Prinses Diana', datetime.date(1961, 6, 1)), ('Tom Cruise', datetime.date(1962, 6, 3)), ('Dalai Lama', datetime.date(1935, 6, 6)), ('O.J. Simpson', datetime.date(1947, 6, 9)), ('William Clark', datetime.date(1770, 8, 1)), ('Yves Saint Laurent', datetime.date(1936, 8, 1)), ('Louis Armstrong', datetime.date(1901, 8, 4)), ('Steve Martin', datetime.date(1945, 8, 14)), ('Ben Affleck', datetime.date(1972, 8, 15)), ('Patrick Swayze', datetime.date(1952, 8, 18)), ('Roman Polanski', datetime.date(1933, 8, 18)), ('Tori Amos', datetime.date(1963, 8, 22)), ('Moeder Teresa', datetime.date(1910, 8, 27)), ('Freddy Mercury', datetime.date(1946, 9, 5)), ('Hugh Grant', datetime.date(1960, 9, 9)), ('Prins Harry', datetime.date(1984, 9, 15)), ('Oliver Stone', datetime.date(1946, 9, 15)), ('Leonard Cohen', datetime.date(1934, 9, 21)), ('Stephen King', datetime.date(1947, 9, 21)), ('Bruce Springsteen', datetime.date(1949, 9, 23)), ('Julie Andrews', datetime.date(1935, 10, 1)), ('Bob Geldof', datetime.date(1954, 10, 5)), ('Kate Winslet', datetime.date(1975, 10, 5)), ('Cliff Richard', datetime.date(1940, 10, 14)), ('Oscar Wilde', datetime.date(1854, 10, 16)), ('Alfred Nobel', datetime.date(1833, 10, 21)), ('Dolph Lungren', datetime.date(1959, 11, 3)), ('Tara Reid', datetime.date(1975, 11, 8)), ('Demi Moore', datetime.date(1962, 11, 11)), ('Leonardo di Caprio', datetime.date(1974, 11, 11)), ('Condoleezza Rice', datetime.date(1954, 11, 14)), ('Prins charles', datetime.date(1948, 11, 14)), ('Isaac Newton', datetime.date(1642, 12, 25))]\n"
]
}
],
"source": [
"import datetime\n",
"\n",
"with open('DataLabo1_01.txt') as infile:\n",
" lines = infile.readlines()\n",
" \n",
"\n",
"def datify(s):\n",
" s = s.replace('\\n','').split('/')\n",
"\n",
" return datetime.date(int(s[2]), int(s[0]), int(s[1]))\n",
"\n",
"\n",
"def parser(lines):\n",
" sol = []\n",
" for l in lines:\n",
" l= l.split(' ')\n",
" try:\n",
" d = datify(l.pop())\n",
" except ValueError:\n",
" print('euh')\n",
" d = 'NaN'\n",
" name = ' '.join(l)\n",
" sol.append((name,d))\n",
" return sol\n",
"\n",
"\n",
"\n",
"sol = parser(lines)\n",
"\n",
"\n",
"print(sol)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sorteren"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 3$: Sorteer de verkregen lijst met tuples op 2 verschillende manieren : eerst op naam, vervolgens op leeftijd. \n",
"\n",
"_Tip_ : Je kan ofwel de $sort$ methode op lijsten gebruiken, ofwel de functie $sorted$ die algemeen werkt op sequences. Probeer beide uit! Merk je een verschil?"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Al Pacino', datetime.date(1940, 4, 25)), ('Albert Einstein', datetime.date(1879, 3, 14)), ('Alfred Nobel', datetime.date(1833, 10, 21)), ('Alice Cooper', datetime.date(1948, 2, 4)), ('Aretha Franklin', datetime.date(1942, 3, 25)), ('Axl Rose', datetime.date(1962, 2, 6)), ('Ben Affleck', datetime.date(1972, 8, 15)), ('Benjamin Franklin', datetime.date(1706, 1, 17)), ('Bette Davies', datetime.date(1908, 4, 5)), ('Betty White', datetime.date(1922, 1, 17)), ('Bob Dylan', datetime.date(1941, 5, 24)), ('Bob Geldof', datetime.date(1954, 10, 5)), ('Bobby Fisher', datetime.date(1943, 3, 9)), ('Brandon Lee', datetime.date(1965, 2, 1)), ('Bridget Fonda', datetime.date(1964, 1, 27)), ('Bruce Springsteen', datetime.date(1949, 9, 23)), ('Charles Dickens', datetime.date(1812, 2, 7)), ('Charles Lindbergh', datetime.date(1902, 2, 4)), ('Cliff Richard', datetime.date(1940, 10, 14)), ('Condoleezza Rice', datetime.date(1954, 11, 14)), ('Dalai Lama', datetime.date(1935, 6, 6)), ('Demi Moore', datetime.date(1962, 11, 11)), ('Dolph Lungren', datetime.date(1959, 11, 3)), ('Dr Benjamin Spock', datetime.date(1903, 5, 2)), ('Elisabeth Taylor', datetime.date(1932, 2, 27)), ('Freddy Mercury', datetime.date(1946, 9, 5)), ('George Lukas', datetime.date(1944, 5, 14)), ('Hugh Grant', datetime.date(1960, 9, 9)), ('Iggy Pop', datetime.date(1947, 4, 21)), ('Isaac Newton', datetime.date(1642, 12, 25)), ('J. Edgar Hoover', datetime.date(1895, 1, 1)), ('James Brown', datetime.date(1933, 5, 3)), ('Jennifer Anniston', datetime.date(1969, 2, 11)), ('John F. Kennedy', datetime.date(1917, 5, 29)), ('John Ford', datetime.date(1895, 2, 1)), ('Johnny Cash', datetime.date(1932, 2, 26)), ('Julie Andrews', datetime.date(1935, 10, 1)), ('Karl Marx', datetime.date(1818, 5, 5)), ('Kate Moss', datetime.date(197, 1, 16)), ('Kate Winslet', datetime.date(1975, 10, 5)), ('Leonard Cohen', datetime.date(1934, 9, 21)), ('Leonardo di Caprio', datetime.date(1974, 11, 11)), ('Louis Armstrong', datetime.date(1901, 8, 4)), ('Madeleine Albright', datetime.date(1937, 5, 15)), ('Marlon Brando', datetime.date(1924, 4, 3)), ('Mel Gibson', datetime.date(1956, 1, 3)), ('Michael Schumacher', datetime.date(1969, 1, 3)), ('Moeder Teresa', datetime.date(1910, 8, 27)), ('Mohammed Ali', datetime.date(1942, 1, 17)), ('Naomi Campbell', datetime.date(1970, 5, 22)), ('Nina Simone', datetime.date(1933, 2, 21)), ('O.J. Simpson', datetime.date(1947, 6, 9)), ('Oliver Stone', datetime.date(1946, 9, 15)), ('Orlando Bloom', datetime.date(1977, 1, 13)), ('Osama Bin Laden', datetime.date(1957, 3, 10)), ('Oscar Wilde', datetime.date(1854, 10, 16)), ('Patrick Swayze', datetime.date(1952, 8, 18)), ('Phill Collins', datetime.date(1951, 1, 30)), ('Prins Harry', datetime.date(1984, 9, 15)), ('Prins charles', datetime.date(1948, 11, 14)), ('Prinses Diana', datetime.date(1961, 6, 1)), ('Richard Nixon', datetime.date(1913, 1, 9)), ('Robert Wagner', datetime.date(1930, 2, 10)), ('Roman Polanski', datetime.date(1933, 8, 18)), ('Ronald Reagon', datetime.date(1911, 2, 6)), ('Russel Crowe', datetime.date(1964, 4, 7)), ('Sigmund Freud', datetime.date(1856, 5, 6)), ('Stephen King', datetime.date(1947, 9, 21)), ('Steve Martin', datetime.date(1945, 8, 14)), ('Steven Jobs', datetime.date(1955, 2, 24)), ('Tara Reid', datetime.date(1975, 11, 8)), ('Tom Cruise', datetime.date(1962, 6, 3)), ('Tori Amos', datetime.date(1963, 8, 22)), ('William Clark', datetime.date(1770, 8, 1)), ('Yves Saint Laurent', datetime.date(1936, 8, 1))]\n"
]
},
{
"data": {
"text/plain": [
"[('Kate Moss', datetime.date(197, 1, 16)),\n",
" ('Isaac Newton', datetime.date(1642, 12, 25)),\n",
" ('Benjamin Franklin', datetime.date(1706, 1, 17)),\n",
" ('William Clark', datetime.date(1770, 8, 1)),\n",
" ('Charles Dickens', datetime.date(1812, 2, 7)),\n",
" ('Karl Marx', datetime.date(1818, 5, 5)),\n",
" ('Alfred Nobel', datetime.date(1833, 10, 21)),\n",
" ('Oscar Wilde', datetime.date(1854, 10, 16)),\n",
" ('Sigmund Freud', datetime.date(1856, 5, 6)),\n",
" ('Albert Einstein', datetime.date(1879, 3, 14)),\n",
" ('J. Edgar Hoover', datetime.date(1895, 1, 1)),\n",
" ('John Ford', datetime.date(1895, 2, 1)),\n",
" ('Louis Armstrong', datetime.date(1901, 8, 4)),\n",
" ('Charles Lindbergh', datetime.date(1902, 2, 4)),\n",
" ('Dr Benjamin Spock', datetime.date(1903, 5, 2)),\n",
" ('Bette Davies', datetime.date(1908, 4, 5)),\n",
" ('Moeder Teresa', datetime.date(1910, 8, 27)),\n",
" ('Ronald Reagon', datetime.date(1911, 2, 6)),\n",
" ('Richard Nixon', datetime.date(1913, 1, 9)),\n",
" ('John F. Kennedy', datetime.date(1917, 5, 29)),\n",
" ('Betty White', datetime.date(1922, 1, 17)),\n",
" ('Marlon Brando', datetime.date(1924, 4, 3)),\n",
" ('Robert Wagner', datetime.date(1930, 2, 10)),\n",
" ('Johnny Cash', datetime.date(1932, 2, 26)),\n",
" ('Elisabeth Taylor', datetime.date(1932, 2, 27)),\n",
" ('Nina Simone', datetime.date(1933, 2, 21)),\n",
" ('James Brown', datetime.date(1933, 5, 3)),\n",
" ('Roman Polanski', datetime.date(1933, 8, 18)),\n",
" ('Leonard Cohen', datetime.date(1934, 9, 21)),\n",
" ('Dalai Lama', datetime.date(1935, 6, 6)),\n",
" ('Julie Andrews', datetime.date(1935, 10, 1)),\n",
" ('Yves Saint Laurent', datetime.date(1936, 8, 1)),\n",
" ('Madeleine Albright', datetime.date(1937, 5, 15)),\n",
" ('Al Pacino', datetime.date(1940, 4, 25)),\n",
" ('Cliff Richard', datetime.date(1940, 10, 14)),\n",
" ('Bob Dylan', datetime.date(1941, 5, 24)),\n",
" ('Mohammed Ali', datetime.date(1942, 1, 17)),\n",
" ('Aretha Franklin', datetime.date(1942, 3, 25)),\n",
" ('Bobby Fisher', datetime.date(1943, 3, 9)),\n",
" ('George Lukas', datetime.date(1944, 5, 14)),\n",
" ('Steve Martin', datetime.date(1945, 8, 14)),\n",
" ('Freddy Mercury', datetime.date(1946, 9, 5)),\n",
" ('Oliver Stone', datetime.date(1946, 9, 15)),\n",
" ('Iggy Pop', datetime.date(1947, 4, 21)),\n",
" ('O.J. Simpson', datetime.date(1947, 6, 9)),\n",
" ('Stephen King', datetime.date(1947, 9, 21)),\n",
" ('Alice Cooper', datetime.date(1948, 2, 4)),\n",
" ('Prins charles', datetime.date(1948, 11, 14)),\n",
" ('Bruce Springsteen', datetime.date(1949, 9, 23)),\n",
" ('Phill Collins', datetime.date(1951, 1, 30)),\n",
" ('Patrick Swayze', datetime.date(1952, 8, 18)),\n",
" ('Bob Geldof', datetime.date(1954, 10, 5)),\n",
" ('Condoleezza Rice', datetime.date(1954, 11, 14)),\n",
" ('Steven Jobs', datetime.date(1955, 2, 24)),\n",
" ('Mel Gibson', datetime.date(1956, 1, 3)),\n",
" ('Osama Bin Laden', datetime.date(1957, 3, 10)),\n",
" ('Dolph Lungren', datetime.date(1959, 11, 3)),\n",
" ('Hugh Grant', datetime.date(1960, 9, 9)),\n",
" ('Prinses Diana', datetime.date(1961, 6, 1)),\n",
" ('Axl Rose', datetime.date(1962, 2, 6)),\n",
" ('Tom Cruise', datetime.date(1962, 6, 3)),\n",
" ('Demi Moore', datetime.date(1962, 11, 11)),\n",
" ('Tori Amos', datetime.date(1963, 8, 22)),\n",
" ('Bridget Fonda', datetime.date(1964, 1, 27)),\n",
" ('Russel Crowe', datetime.date(1964, 4, 7)),\n",
" ('Brandon Lee', datetime.date(1965, 2, 1)),\n",
" ('Michael Schumacher', datetime.date(1969, 1, 3)),\n",
" ('Jennifer Anniston', datetime.date(1969, 2, 11)),\n",
" ('Naomi Campbell', datetime.date(1970, 5, 22)),\n",
" ('Ben Affleck', datetime.date(1972, 8, 15)),\n",
" ('Leonardo di Caprio', datetime.date(1974, 11, 11)),\n",
" ('Kate Winslet', datetime.date(1975, 10, 5)),\n",
" ('Tara Reid', datetime.date(1975, 11, 8)),\n",
" ('Orlando Bloom', datetime.date(1977, 1, 13)),\n",
" ('Prins Harry', datetime.date(1984, 9, 15))]"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"\n",
"with open('DataLabo1_01.txt') as infile:\n",
" lines = infile.readlines()\n",
" \n",
"\n",
"def datify(s):\n",
" s = s.replace('\\n','').split('/')\n",
"\n",
" return datetime.date(int(s[2]), int(s[0]), int(s[1]))\n",
"\n",
"\n",
"def parser(lines):\n",
" sol = []\n",
" for l in lines:\n",
" l= l.split(' ')\n",
" try:\n",
" d = datify(l.pop())\n",
" except ValueError:\n",
" print('euh')\n",
" d = 'NaN'\n",
" name = ' '.join(l)\n",
" sol.append((name,d))\n",
" return sol\n",
"\n",
"\n",
"\n",
"sol = parser(lines)\n",
"\n",
"def sortName(val):\n",
" return val[0]\n",
"\n",
"def sortDate(val):\n",
" return val[1]\n",
"\n",
"sol.sort(key = sortName)\n",
"print(sol)\n",
"sorted(sol, key=sortDate)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Sets"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 4$ : \n",
"Breid de bovenstaande lijst uit met volgende personen :\n",
"\n",
"- Roger Federer 1981/08/08\n",
"- Dustin Hoffman 1937/08/08\n",
"- Vanessa Amorosi 1981/08/08\n",
"- Whitney Houston 1963/08/09\n",
"- Meat Loaf 1947/09/27\n",
"- Cheryl Tiegs 1947/09/27\n",
"- Robert Redford 1936/08/18\n",
"\n",
"\n",
"Maak nu volgende verzamelingen :\n",
"\n",
"* Maak voor elke maand een verzameling van personen die in deze maand van het jaar jarig zijn. Stop je resultaat in een dict als volgt : key = maand; value= verzameling personen.\n",
"* Maak voor elk jaartal (aanwezig in de data) een verzameling van personen die in dat jaar geboren zijn. Stop je resultaat in een dict als volgt : key = jaartal; value= verzameling personen.\n",
"* Maak voor elke dag in het jaar een verzameling van personen die op deze dag van het jaar jarig zijn. Stop je resultaat in een dict als volgt : key = (dag,maand); value= verzameling personen.\n",
"\n",
"Gebruik makend van bovenstaande structuren kan je eenvoudig volgende queries beantwoorden :\n",
"\n",
"* Geef alle mensen die in oktober jarig zijn\n",
"* In welk jaartal(len) zijn de meeste mensen geboren volgens deze data file?\n",
"* Wie is er jarig op 18 augustus?\n",
"\n",
"_Tip_ : Voor het aanmaken van dicts kan je handig gebruik maken van $defaultdict$\n"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Albert Einstein', datetime.date(1879, 3, 14)), ('Benjamin Franklin', datetime.date(1706, 1, 17)), ('J. Edgar Hoover', datetime.date(1895, 1, 1)), ('Mel Gibson', datetime.date(1956, 1, 3)), ('Michael Schumacher', datetime.date(1969, 1, 3)), ('Richard Nixon', datetime.date(1913, 1, 9)), ('Orlando Bloom', datetime.date(1977, 1, 13)), ('Kate Moss', datetime.date(197, 1, 16)), ('Betty White', datetime.date(1922, 1, 17)), ('Mohammed Ali', datetime.date(1942, 1, 17)), ('Bridget Fonda', datetime.date(1964, 1, 27)), ('Phill Collins', datetime.date(1951, 1, 30)), ('John Ford', datetime.date(1895, 2, 1)), ('Brandon Lee', datetime.date(1965, 2, 1)), ('Charles Lindbergh', datetime.date(1902, 2, 4)), ('Alice Cooper', datetime.date(1948, 2, 4)), ('Ronald Reagon', datetime.date(1911, 2, 6)), ('Axl Rose', datetime.date(1962, 2, 6)), ('Charles Dickens', datetime.date(1812, 2, 7)), ('Robert Wagner', datetime.date(1930, 2, 10)), ('Jennifer Anniston', datetime.date(1969, 2, 11)), ('Nina Simone', datetime.date(1933, 2, 21)), ('Steven Jobs', datetime.date(1955, 2, 24)), ('Johnny Cash', datetime.date(1932, 2, 26)), ('Elisabeth Taylor', datetime.date(1932, 2, 27)), ('Bobby Fisher', datetime.date(1943, 3, 9)), ('Osama Bin Laden', datetime.date(1957, 3, 10)), ('Aretha Franklin', datetime.date(1942, 3, 25)), ('Marlon Brando', datetime.date(1924, 4, 3)), ('Bette Davies', datetime.date(1908, 4, 5)), ('Russel Crowe', datetime.date(1964, 4, 7)), ('Iggy Pop', datetime.date(1947, 4, 21)), ('Al Pacino', datetime.date(1940, 4, 25)), ('James Brown', datetime.date(1933, 5, 3)), ('Dr Benjamin Spock', datetime.date(1903, 5, 2)), ('Karl Marx', datetime.date(1818, 5, 5)), ('Sigmund Freud', datetime.date(1856, 5, 6)), ('George Lukas', datetime.date(1944, 5, 14)), ('Madeleine Albright', datetime.date(1937, 5, 15)), ('Naomi Campbell', datetime.date(1970, 5, 22)), ('Bob Dylan', datetime.date(1941, 5, 24)), ('John F. Kennedy', datetime.date(1917, 5, 29)), ('Prinses Diana', datetime.date(1961, 6, 1)), ('Tom Cruise', datetime.date(1962, 6, 3)), ('Dalai Lama', datetime.date(1935, 6, 6)), ('O.J. Simpson', datetime.date(1947, 6, 9)), ('William Clark', datetime.date(1770, 8, 1)), ('Yves Saint Laurent', datetime.date(1936, 8, 1)), ('Louis Armstrong', datetime.date(1901, 8, 4)), ('Steve Martin', datetime.date(1945, 8, 14)), ('Ben Affleck', datetime.date(1972, 8, 15)), ('Patrick Swayze', datetime.date(1952, 8, 18)), ('Roman Polanski', datetime.date(1933, 8, 18)), ('Tori Amos', datetime.date(1963, 8, 22)), ('Moeder Teresa', datetime.date(1910, 8, 27)), ('Freddy Mercury', datetime.date(1946, 9, 5)), ('Hugh Grant', datetime.date(1960, 9, 9)), ('Prins Harry', datetime.date(1984, 9, 15)), ('Oliver Stone', datetime.date(1946, 9, 15)), ('Leonard Cohen', datetime.date(1934, 9, 21)), ('Stephen King', datetime.date(1947, 9, 21)), ('Bruce Springsteen', datetime.date(1949, 9, 23)), ('Julie Andrews', datetime.date(1935, 10, 1)), ('Bob Geldof', datetime.date(1954, 10, 5)), ('Kate Winslet', datetime.date(1975, 10, 5)), ('Cliff Richard', datetime.date(1940, 10, 14)), ('Oscar Wilde', datetime.date(1854, 10, 16)), ('Alfred Nobel', datetime.date(1833, 10, 21)), ('Dolph Lungren', datetime.date(1959, 11, 3)), ('Tara Reid', datetime.date(1975, 11, 8)), ('Demi Moore', datetime.date(1962, 11, 11)), ('Leonardo di Caprio', datetime.date(1974, 11, 11)), ('Condoleezza Rice', datetime.date(1954, 11, 14)), ('Prins charles', datetime.date(1948, 11, 14)), ('Isaac Newton', datetime.date(1642, 12, 25)), ('Roger Federer', datetime.date(1981, 8, 8)), ('Dustin Hoffman', datetime.date(1937, 8, 8)), ('Vanessa Amorosi', datetime.date(1981, 8, 8)), ('Whitney Houston', datetime.date(1963, 8, 9)), ('Meat Loaf', datetime.date(1947, 9, 27)), ('Cheryl Tiegs', datetime.date(1947, 9, 27)), ('Robert Redford', datetime.date(1936, 8, 18))]\n",
"[('Al Pacino', datetime.date(1940, 4, 25)), ('Albert Einstein', datetime.date(1879, 3, 14)), ('Alfred Nobel', datetime.date(1833, 10, 21)), ('Alice Cooper', datetime.date(1948, 2, 4)), ('Aretha Franklin', datetime.date(1942, 3, 25)), ('Axl Rose', datetime.date(1962, 2, 6)), ('Ben Affleck', datetime.date(1972, 8, 15)), ('Benjamin Franklin', datetime.date(1706, 1, 17)), ('Bette Davies', datetime.date(1908, 4, 5)), ('Betty White', datetime.date(1922, 1, 17)), ('Bob Dylan', datetime.date(1941, 5, 24)), ('Bob Geldof', datetime.date(1954, 10, 5)), ('Bobby Fisher', datetime.date(1943, 3, 9)), ('Brandon Lee', datetime.date(1965, 2, 1)), ('Bridget Fonda', datetime.date(1964, 1, 27)), ('Bruce Springsteen', datetime.date(1949, 9, 23)), ('Charles Dickens', datetime.date(1812, 2, 7)), ('Charles Lindbergh', datetime.date(1902, 2, 4)), ('Cheryl Tiegs', datetime.date(1947, 9, 27)), ('Cliff Richard', datetime.date(1940, 10, 14)), ('Condoleezza Rice', datetime.date(1954, 11, 14)), ('Dalai Lama', datetime.date(1935, 6, 6)), ('Demi Moore', datetime.date(1962, 11, 11)), ('Dolph Lungren', datetime.date(1959, 11, 3)), ('Dr Benjamin Spock', datetime.date(1903, 5, 2)), ('Dustin Hoffman', datetime.date(1937, 8, 8)), ('Elisabeth Taylor', datetime.date(1932, 2, 27)), ('Freddy Mercury', datetime.date(1946, 9, 5)), ('George Lukas', datetime.date(1944, 5, 14)), ('Hugh Grant', datetime.date(1960, 9, 9)), ('Iggy Pop', datetime.date(1947, 4, 21)), ('Isaac Newton', datetime.date(1642, 12, 25)), ('J. Edgar Hoover', datetime.date(1895, 1, 1)), ('James Brown', datetime.date(1933, 5, 3)), ('Jennifer Anniston', datetime.date(1969, 2, 11)), ('John F. Kennedy', datetime.date(1917, 5, 29)), ('John Ford', datetime.date(1895, 2, 1)), ('Johnny Cash', datetime.date(1932, 2, 26)), ('Julie Andrews', datetime.date(1935, 10, 1)), ('Karl Marx', datetime.date(1818, 5, 5)), ('Kate Moss', datetime.date(197, 1, 16)), ('Kate Winslet', datetime.date(1975, 10, 5)), ('Leonard Cohen', datetime.date(1934, 9, 21)), ('Leonardo di Caprio', datetime.date(1974, 11, 11)), ('Louis Armstrong', datetime.date(1901, 8, 4)), ('Madeleine Albright', datetime.date(1937, 5, 15)), ('Marlon Brando', datetime.date(1924, 4, 3)), ('Meat Loaf', datetime.date(1947, 9, 27)), ('Mel Gibson', datetime.date(1956, 1, 3)), ('Michael Schumacher', datetime.date(1969, 1, 3)), ('Moeder Teresa', datetime.date(1910, 8, 27)), ('Mohammed Ali', datetime.date(1942, 1, 17)), ('Naomi Campbell', datetime.date(1970, 5, 22)), ('Nina Simone', datetime.date(1933, 2, 21)), ('O.J. Simpson', datetime.date(1947, 6, 9)), ('Oliver Stone', datetime.date(1946, 9, 15)), ('Orlando Bloom', datetime.date(1977, 1, 13)), ('Osama Bin Laden', datetime.date(1957, 3, 10)), ('Oscar Wilde', datetime.date(1854, 10, 16)), ('Patrick Swayze', datetime.date(1952, 8, 18)), ('Phill Collins', datetime.date(1951, 1, 30)), ('Prins Harry', datetime.date(1984, 9, 15)), ('Prins charles', datetime.date(1948, 11, 14)), ('Prinses Diana', datetime.date(1961, 6, 1)), ('Richard Nixon', datetime.date(1913, 1, 9)), ('Robert Redford', datetime.date(1936, 8, 18)), ('Robert Wagner', datetime.date(1930, 2, 10)), ('Roger Federer', datetime.date(1981, 8, 8)), ('Roman Polanski', datetime.date(1933, 8, 18)), ('Ronald Reagon', datetime.date(1911, 2, 6)), ('Russel Crowe', datetime.date(1964, 4, 7)), ('Sigmund Freud', datetime.date(1856, 5, 6)), ('Stephen King', datetime.date(1947, 9, 21)), ('Steve Martin', datetime.date(1945, 8, 14)), ('Steven Jobs', datetime.date(1955, 2, 24)), ('Tara Reid', datetime.date(1975, 11, 8)), ('Tom Cruise', datetime.date(1962, 6, 3)), ('Tori Amos', datetime.date(1963, 8, 22)), ('Vanessa Amorosi', datetime.date(1981, 8, 8)), ('Whitney Houston', datetime.date(1963, 8, 9)), ('William Clark', datetime.date(1770, 8, 1)), ('Yves Saint Laurent', datetime.date(1936, 8, 1))]\n"
]
},
{
"data": {
"text/plain": [
"[('Kate Moss', datetime.date(197, 1, 16)),\n",
" ('Isaac Newton', datetime.date(1642, 12, 25)),\n",
" ('Benjamin Franklin', datetime.date(1706, 1, 17)),\n",
" ('William Clark', datetime.date(1770, 8, 1)),\n",
" ('Charles Dickens', datetime.date(1812, 2, 7)),\n",
" ('Karl Marx', datetime.date(1818, 5, 5)),\n",
" ('Alfred Nobel', datetime.date(1833, 10, 21)),\n",
" ('Oscar Wilde', datetime.date(1854, 10, 16)),\n",
" ('Sigmund Freud', datetime.date(1856, 5, 6)),\n",
" ('Albert Einstein', datetime.date(1879, 3, 14)),\n",
" ('J. Edgar Hoover', datetime.date(1895, 1, 1)),\n",
" ('John Ford', datetime.date(1895, 2, 1)),\n",
" ('Louis Armstrong', datetime.date(1901, 8, 4)),\n",
" ('Charles Lindbergh', datetime.date(1902, 2, 4)),\n",
" ('Dr Benjamin Spock', datetime.date(1903, 5, 2)),\n",
" ('Bette Davies', datetime.date(1908, 4, 5)),\n",
" ('Moeder Teresa', datetime.date(1910, 8, 27)),\n",
" ('Ronald Reagon', datetime.date(1911, 2, 6)),\n",
" ('Richard Nixon', datetime.date(1913, 1, 9)),\n",
" ('John F. Kennedy', datetime.date(1917, 5, 29)),\n",
" ('Betty White', datetime.date(1922, 1, 17)),\n",
" ('Marlon Brando', datetime.date(1924, 4, 3)),\n",
" ('Robert Wagner', datetime.date(1930, 2, 10)),\n",
" ('Johnny Cash', datetime.date(1932, 2, 26)),\n",
" ('Elisabeth Taylor', datetime.date(1932, 2, 27)),\n",
" ('Nina Simone', datetime.date(1933, 2, 21)),\n",
" ('James Brown', datetime.date(1933, 5, 3)),\n",
" ('Roman Polanski', datetime.date(1933, 8, 18)),\n",
" ('Leonard Cohen', datetime.date(1934, 9, 21)),\n",
" ('Dalai Lama', datetime.date(1935, 6, 6)),\n",
" ('Julie Andrews', datetime.date(1935, 10, 1)),\n",
" ('Yves Saint Laurent', datetime.date(1936, 8, 1)),\n",
" ('Robert Redford', datetime.date(1936, 8, 18)),\n",
" ('Madeleine Albright', datetime.date(1937, 5, 15)),\n",
" ('Dustin Hoffman', datetime.date(1937, 8, 8)),\n",
" ('Al Pacino', datetime.date(1940, 4, 25)),\n",
" ('Cliff Richard', datetime.date(1940, 10, 14)),\n",
" ('Bob Dylan', datetime.date(1941, 5, 24)),\n",
" ('Mohammed Ali', datetime.date(1942, 1, 17)),\n",
" ('Aretha Franklin', datetime.date(1942, 3, 25)),\n",
" ('Bobby Fisher', datetime.date(1943, 3, 9)),\n",
" ('George Lukas', datetime.date(1944, 5, 14)),\n",
" ('Steve Martin', datetime.date(1945, 8, 14)),\n",
" ('Freddy Mercury', datetime.date(1946, 9, 5)),\n",
" ('Oliver Stone', datetime.date(1946, 9, 15)),\n",
" ('Iggy Pop', datetime.date(1947, 4, 21)),\n",
" ('O.J. Simpson', datetime.date(1947, 6, 9)),\n",
" ('Stephen King', datetime.date(1947, 9, 21)),\n",
" ('Cheryl Tiegs', datetime.date(1947, 9, 27)),\n",
" ('Meat Loaf', datetime.date(1947, 9, 27)),\n",
" ('Alice Cooper', datetime.date(1948, 2, 4)),\n",
" ('Prins charles', datetime.date(1948, 11, 14)),\n",
" ('Bruce Springsteen', datetime.date(1949, 9, 23)),\n",
" ('Phill Collins', datetime.date(1951, 1, 30)),\n",
" ('Patrick Swayze', datetime.date(1952, 8, 18)),\n",
" ('Bob Geldof', datetime.date(1954, 10, 5)),\n",
" ('Condoleezza Rice', datetime.date(1954, 11, 14)),\n",
" ('Steven Jobs', datetime.date(1955, 2, 24)),\n",
" ('Mel Gibson', datetime.date(1956, 1, 3)),\n",
" ('Osama Bin Laden', datetime.date(1957, 3, 10)),\n",
" ('Dolph Lungren', datetime.date(1959, 11, 3)),\n",
" ('Hugh Grant', datetime.date(1960, 9, 9)),\n",
" ('Prinses Diana', datetime.date(1961, 6, 1)),\n",
" ('Axl Rose', datetime.date(1962, 2, 6)),\n",
" ('Tom Cruise', datetime.date(1962, 6, 3)),\n",
" ('Demi Moore', datetime.date(1962, 11, 11)),\n",
" ('Whitney Houston', datetime.date(1963, 8, 9)),\n",
" ('Tori Amos', datetime.date(1963, 8, 22)),\n",
" ('Bridget Fonda', datetime.date(1964, 1, 27)),\n",
" ('Russel Crowe', datetime.date(1964, 4, 7)),\n",
" ('Brandon Lee', datetime.date(1965, 2, 1)),\n",
" ('Michael Schumacher', datetime.date(1969, 1, 3)),\n",
" ('Jennifer Anniston', datetime.date(1969, 2, 11)),\n",
" ('Naomi Campbell', datetime.date(1970, 5, 22)),\n",
" ('Ben Affleck', datetime.date(1972, 8, 15)),\n",
" ('Leonardo di Caprio', datetime.date(1974, 11, 11)),\n",
" ('Kate Winslet', datetime.date(1975, 10, 5)),\n",
" ('Tara Reid', datetime.date(1975, 11, 8)),\n",
" ('Orlando Bloom', datetime.date(1977, 1, 13)),\n",
" ('Roger Federer', datetime.date(1981, 8, 8)),\n",
" ('Vanessa Amorosi', datetime.date(1981, 8, 8)),\n",
" ('Prins Harry', datetime.date(1984, 9, 15))]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import datetime\n",
"\n",
"with open('DataLabo1_01.txt') as infile:\n",
" lines = infile.readlines()\n",
" \n",
"\n",
"def datify(s):\n",
" s = s.replace('\\n','').split('/')\n",
"\n",
" return datetime.date(int(s[2]), int(s[0]), int(s[1]))\n",
"\n",
"\n",
"def parser(lines):\n",
" sol = []\n",
" for l in lines:\n",
" l= l.split(' ')\n",
" try:\n",
" d = datify(l.pop())\n",
" except ValueError:\n",
" print('euh')\n",
" d = 'NaN'\n",
" name = ' '.join(l)\n",
" sol.append((name,d))\n",
" return sol\n",
"\n",
"f = [('Roger Federer', datetime.date(1981,8,8)),\n",
"('Dustin Hoffman', datetime.date(1937,8,8)),\n",
" ('Vanessa Amorosi', datetime.date(1981,8,8)),\n",
"('Whitney Houston', datetime.date(1963,8,9)),\n",
"('Meat Loaf', datetime.date(1947,9,27)),\n",
"('Cheryl Tiegs', datetime.date(1947,9,27)),\n",
"('Robert Redford',datetime.date(1936,8,18))]\n",
"\n",
"sol = parser(lines)\n",
"sol.extend(f)\n",
"\n",
"print(sol)\n",
"#print(sol)\n",
"def sortName(val):\n",
" return val[0]\n",
"\n",
"def sortDate(val):\n",
" return val[1]\n",
"\n",
"sol.sort(key = sortName)\n",
"print(sol)\n",
"sorted(sol, key=sortDate)\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Generator functies"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Een generator functie is een manier in Python om een itereerbaar object te maken. Ze geven een sequentie van resultaten weer maar op een luie manier. De resultaten worden pas teruggegeven wanneer er expliciet naar gevraagd wordt via bv. de next() methode. Een generator zal ipv het $return$ keyword gebruik maken van het keyword $yield$. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<generator object squares at 0x7f845c590c78>\n",
"1\n",
"De for lus begint nu vanaf : \n",
"4\n",
"9\n",
"16\n",
"25\n",
"36\n",
"49\n",
"64\n",
"81\n",
"100\n"
]
}
],
"source": [
"#voorbeeld van een generator functie\n",
"def squares(n=10):\n",
" for i in range(1,n+1):\n",
" yield i**2\n",
"\n",
"gen = squares()\n",
"#geef het volgende getal in de sequentie\n",
"print(gen)\n",
"print(next(gen))\n",
"\n",
"print('De for lus begint nu vanaf : ')\n",
"for x in gen:\n",
" print(x)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 5$: \n",
"\n",
"* Schrijf een functie $birth\\_year\\_month\\_day$ die gegeven een jaartal, een maand en een dag alle personen teruggeven die op deze dag geboren werden.\n",
"* Schrijf een generator functie $gen\\_same\\_anniversaries$ die alle (dag,maand) tuples zal weergeven waarop er meer dan 1 persoon jarig is\n",
"* Schrijf een generator functie $gen\\_same\\_birthdays$ die alle (jaar,maand,dag) tuples zal weergeven waarop er meer dan 1 persoon jarig is\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Mohammed Ali', datetime.date(1942, 1, 17)), ('David Bowie', datetime.date(1942, 1, 17))]\n",
"('Mohammed Ali', datetime.date(1942, 1, 17))\n",
"('David Bowie', datetime.date(1942, 1, 17))\n"
]
}
],
"source": [
"\n",
"import datetime\n",
"\n",
"\n",
"\n",
"\n",
"def birth_year_month_day(y,m,d,db):\n",
" sol = []\n",
" if (y,m,d) in gen_same_birthdays(db):\n",
" for i in db:\n",
" if i[1] == datetime.date(y,m,d):\n",
" sol.append(i)\n",
" return sol\n",
"\n",
"\n",
"#y do dis?\n",
"def gen_same_birthdays(db):\n",
" temp = [(i[1].year, i[1].month, i[1].day) for i in db]\n",
" for i in temp:\n",
" if temp.count(i) > 1:\n",
" yield i\n",
" \n",
" \n",
"def gen_same_anniversaries(db):\n",
" temp = [(i[1].day, i[1].month) for i in db]\n",
" for i in temp:\n",
" if temp.count(i) > 1:\n",
" yield i\n",
"\n",
"with open('DataLabo1_01.txt') as infile:\n",
" lines = infile.readlines()\n",
" \n",
"\n",
"def datify(s):\n",
" s = s.replace('\\n','').split('/')\n",
" return datetime.date(int(s[2]), int(s[0]), int(s[1]))\n",
"\n",
"\n",
"def parser(lines):\n",
" sol = []\n",
" for l in lines:\n",
" l= l.split(' ')\n",
" try:\n",
" d = datify(l.pop())\n",
" except ValueError:\n",
" print('euh')\n",
" d = 'NaN'\n",
" name = ' '.join(l)\n",
" sol.append((name,d))\n",
" return sol\n",
"\n",
"f = [('Roger Federer', datetime.date(1981,8,8)),\n",
"('Dustin Hoffman', datetime.date(1937,8,8)),\n",
" ('Vanessa Amorosi', datetime.date(1981,8,8)),\n",
"('Whitney Houston', datetime.date(1963,8,9)),\n",
"('Meat Loaf', datetime.date(1947,9,27)),\n",
"('Cheryl Tiegs', datetime.date(1947,9,27)),\n",
"('Robert Redford',datetime.date(1936,8,18)),\n",
"('David Bowie', datetime.date(1942,1,17))]\n",
"\n",
"sol = parser(lines)\n",
"sol.extend(f)\n",
"temp = gen_same_birthdays(sol)\n",
"temp = gen_same_anniversaries(sol)\n",
"temp = birth_year_month_day(1942,1,17,sol)\n",
"print(temp)\n",
"for i in temp:\n",
" print(i)\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plotting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 6$ : Maak een scatter plot over de verspreiding van de verjaardagen. De X-as stelt de maanden voor, de Y-as de dagen (begrens deze!). Maak je plot 15 op 10, en duid alle verjaardagen aan met een rode dot, alle verjaardagen waarop meerdere mensen vjarig zijn met een zwarte diamant en alle dagen waarop mensen een zelfde geboortedatum delen met een blauwe ster. Voeg een legende toe.\n",
"\n",
"_Tip_ : gebruik bovenstaand generatorfuncties om de juiste data te maken voor je plots. Via ´zip(*data)´ kan je lijsten van tuples unpacken."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Matrices"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Een matrix zou je in Python kunnen voorstellen als een geneste lijst. \n",
"Je kan op een eenvoudige manier elementen accesereren (via de indices, slicing), elementen wijzigen, deleten enz.."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"T = [[11, 12, 5, 2], [15, 6, 10, 15], [10, 8, 12, 5], [12,15,8,6]]\n",
"print(T[0])\n",
"print(T[1][2])\n",
"\n",
"for r in T:\n",
" for c in r:\n",
" print(c,end = \" \")\n",
" print()\n",
" \n",
"print('\\ninserting:' + str([0,5,11,13,6]))\n",
"#rij toevoegen\n",
"T.insert(2, [0,5,11,13,6])\n",
"\n",
"for r in T:\n",
" for c in r:\n",
" print(c,end = \" \")\n",
" print()\n",
"\n",
"print('\\nupdating:')\n",
"# wijzig de tweede rij naar [11,9,7]\n",
"...\n",
"# het element op positie(0,3) naar 100\n",
"...\n",
"\n",
"print('\\ndeleting')\n",
"#verwijder de derde rij\n",
"...\n",
"#verwijder het element op postie(0,0)\n",
"...\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Om te rekenen met matrices echter is de numpy module aangewezen. Ook hierin wordt onderscheid gemaakt tussen een matrix en een ndarray."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"#vector\n",
"x = np.array([1,2])\n",
"\n",
"#matrix\n",
"m = np.mat( ((2,3), (3, 5)) )\n",
"\n",
"#array\n",
"y = np.array( ((1,2), (5, -1)) )\n",
"\n",
"print(x, x.shape,x.dtype)\n",
"print(m, m.shape, m.dtype)\n",
"print(n,n.shape,n.dtype)\n",
"print(y, y.shape,y.dtype)\n",
"\n",
"print(x+x)\n",
"\n",
"print('\\n', y*y)\n",
"print(np.dot(y,y))\n",
"print(y@y)\n",
"\n",
"print('\\n', m*m)\n",
"print(np.dot(m,m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$Oefening 7$ : Vraagstuk : 4 personen kopen elk respectivelijk volgende hoeveelheid paaseieren\n",
" * persoon 1 : 100g witte, 175gr bruine, 210gr zwarte\n",
" * persoon 2 : 90g witte, 160gr bruine, 150gr zwarte\n",
" * persoon 3 : 200g witte, 50gr bruine, 100gr zwarte\n",
" * persoon 4 : 120g witte, 310gr zwarte\n",
"De volgende prijzen worden gehanteerd :\n",
"* witte chocolade : 2.98 euro / 100g\n",
"* bruine chocolade : 1.99 euro /100g\n",
"* zwarte chocolade : 3.90 euro /100g\n",
"\n",
"Bereken via matrix operaties in numpy hoeveel euro elk van hen zal betalen."
]
},
{
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}