Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
gf_informatik:daten:processing [2023-05-24 12:15] – [Aufgabe 2] hof | gf_informatik:daten:processing [2024-06-10 10:45] (aktuell) – hof | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
# Data Processing in Python | # Data Processing in Python | ||
+ | |||
+ | {{ : | ||
Wir haben nun einige Erfahrung gesammelt, um mit Tabellenprogrammen Daten zu bearbeiten und darzustellen. Aber was, wenn wir *mehr* Daten haben, als Excel darstellen kann? | Wir haben nun einige Erfahrung gesammelt, um mit Tabellenprogrammen Daten zu bearbeiten und darzustellen. Aber was, wenn wir *mehr* Daten haben, als Excel darstellen kann? | ||
Wieviel ist viel? 1000? | Wieviel ist viel? 1000? | ||
+ | |||
#### Aufgabe 1 | #### Aufgabe 1 | ||
Zeile 71: | Zeile 74: | ||
* Mit `print(values[1])` wird der Kanton (die zweite Spalte) ausgegeben. | * Mit `print(values[1])` wird der Kanton (die zweite Spalte) ausgegeben. | ||
* Wieviele Gemeinden gibt es im Kanton Thurgau? Wieviele im Kanton Bern? | * Wieviele Gemeinden gibt es im Kanton Thurgau? Wieviele im Kanton Bern? | ||
+ | |||
+ | <nodisp 1> | ||
+ | ++++Lösung: | ||
+ | <code python> | ||
+ | with open(' | ||
+ | be = 0 | ||
+ | tg = 0 | ||
+ | for line in infile: | ||
+ | tokens = line.split(',' | ||
+ | canton = tokens[1] | ||
+ | if canton == ' | ||
+ | tg = tg + 1 | ||
+ | elif canton == ' | ||
+ | be = be + 1 | ||
+ | print(" | ||
+ | print(" | ||
+ | </ | ||
+ | ++++ | ||
+ | </ | ||
Zeile 112: | Zeile 134: | ||
def find_smallest_population(): | def find_smallest_population(): | ||
with open(' | with open(' | ||
+ | # Search for the smallest, so start out with a value larger than any expected. | ||
min_pop = 1000000 | min_pop = 1000000 | ||
town = None | town = None | ||
Zeile 117: | Zeile 140: | ||
cells = line.split(',' | cells = line.split(',' | ||
try: | try: | ||
+ | # Population is in the third column, and it is an integer. | ||
population = int(cells[2]) | population = int(cells[2]) | ||
if population < min_pop: | if population < min_pop: | ||
+ | # We found a town smaller than the currently known smallest. | ||
+ | min_pop = population | ||
+ | town = cells[0] | ||
+ | except ValueError: | ||
+ | pass | ||
+ | return town, min_pop | ||
+ | |||
+ | print(" | ||
+ | </ | ||
+ | ++++ | ||
+ | </ | ||
+ | |||
+ | <nodisp 2> | ||
+ | ++++Code:| | ||
+ | <code python> | ||
+ | def find_smallest_population(): | ||
+ | with open(' | ||
+ | # Search for the smallest, so start out with a value larger than any expected. | ||
+ | min_pop = 1000000 | ||
+ | town = None | ||
+ | for line in towns: | ||
+ | cells = line.split(',' | ||
+ | try: | ||
+ | # Population is in the third column, and it is an integer. | ||
+ | population = int(cells[2]) | ||
+ | if population < min_pop: | ||
+ | # We found a town smaller than the currently known smallest. | ||
min_pop = population | min_pop = population | ||
town = cells[0] | town = cells[0] | ||
Zeile 129: | Zeile 180: | ||
def find_largest_area(): | def find_largest_area(): | ||
with open(' | with open(' | ||
+ | # We search for the largest, so start with a small value. | ||
max_area = 0 | max_area = 0 | ||
town = None | town = None | ||
Zeile 134: | Zeile 186: | ||
cells = line.split(',' | cells = line.split(',' | ||
try: | try: | ||
+ | # Area is in the fourth column, and it is a floating point number. | ||
area = float(cells[3]) | area = float(cells[3]) | ||
if area > max_area: | if area > max_area: | ||
+ | # Found a town with larger area than the largest known so far. | ||
max_area = area | max_area = area | ||
town = cells[0] | town = cells[0] | ||
Zeile 153: | Zeile 207: | ||
* Wieviele Gemeinden hat der Kanton Glarus? | * Wieviele Gemeinden hat der Kanton Glarus? | ||
- | < | + | < |
++++Code:| | ++++Code:| | ||
<code python> | <code python> |