Created
May 10, 2019 14:23
-
-
Save EmilHernvall/eaf061ace362efa5b12bb8cae4ee1b82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
def columns_to_county(foo): | |
county = {} | |
county["id"] = int(foo[0]) | |
county["name"] = foo[1] | |
county["lon"] = float(foo[3]) | |
county["lat"] = float(foo[4]) | |
return county | |
fh = open("data/lan.csv", "r", encoding="utf-8") | |
data = fh.read() | |
lines = data.split("\n") | |
counties = [] | |
for line in lines: | |
columns = line.split(";") | |
county = columns_to_county(columns) | |
counties.append(county) | |
print("Number of counties: ", len(counties)) | |
def get_sort_key(county_dict): | |
return county_dict["lat"] | |
print("From south to north:") | |
counties = sorted(counties, key=get_sort_key) | |
for county in counties: | |
print(county["name"]) | |
print() | |
part_of_sweden = {} | |
part_of_sweden["G"] = [] | |
part_of_sweden["S"] = [] | |
part_of_sweden["N"] = [] | |
for county in counties: | |
if county["lat"] > 60.78: | |
part = "N" | |
elif county["lat"] > 58.41: | |
part = "S" | |
else: | |
part = "G" | |
part_of_sweden[part].append(county) | |
print("Götaland:") | |
for county in part_of_sweden["G"]: | |
print(county["name"]) | |
print() | |
print("Svealand:") | |
for county in part_of_sweden["S"]: | |
print(county["name"]) | |
print() | |
print("Norrland:") | |
for county in part_of_sweden["N"]: | |
print(county["name"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
with open("data/postnummer.txt", "r", encoding="utf-8") as fh: | |
data = fh.read() | |
lines = data.split("\n") | |
zip_codes = [] | |
for line in lines: | |
columns = line.split(";") | |
zip_code = { | |
"postnr": columns[0], | |
"kommun": columns[1], | |
"lankod": columns[2], | |
"lannamn": columns[3], | |
"kommunkod": columns[4], | |
"kommunnamn": columns[5], | |
} | |
zip_codes.append(zip_code) | |
kommuntabell = defaultdict(list) | |
for zip_code in zip_codes: | |
kommuntabell[zip_code["kommunkod"]].append(zip_code["postnr"]) | |
kommunlista = [] | |
for kommunkod, postnr in kommuntabell.items(): | |
kommun = {} | |
kommun["kod"] = kommunkod | |
kommun["postnr"] = postnr | |
kommun["antal"] = len(postnr) | |
kommunlista.append(kommun) | |
def sort_by_antal(kommun): | |
return kommun["antal"] | |
kommunlista = sorted(kommunlista, key=sort_by_antal, reverse=True) | |
for i, kommun in enumerate(kommunlista): | |
if i <= 10: | |
print(kommun["kod"], kommun["antal"]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
with open("data/postnummer.txt", "r", encoding="utf-8") as fh: | |
data = fh.read() | |
lines = data.split("\n") | |
zip_codes = [] | |
for line in lines: | |
columns = line.split(";") | |
zip_code = { | |
"postnr": columns[0], | |
"kommun": columns[1], | |
"lankod": columns[2], | |
"lannamn": columns[3], | |
"kommunkod": columns[4], | |
"kommunnamn": columns[5], | |
} | |
zip_codes.append(zip_code) | |
postnrtabell = defaultdict(list) | |
for zip_code in zip_codes: | |
postnrtabell[zip_code["postnr"]].append(zip_code["kommunkod"]) | |
for postnr, kommuner in postnrtabell.items(): | |
if len(kommuner) > 1: | |
print(postnr, kommuner) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment