Dictionary
Verfasst: Mittwoch 28. November 2018, 14:58
Hallo Leute,
im Grunde habe ich nur eine kurze spezifische Frage bezüglich einer Funktion die mir ein Dictionary erstellt.
Die Funktion:
Die Funktion erstellt mir ein Dictionary der Art:
health_profile = {"Max": {"Birth state": "Bayern", "Gender": "Male", "Resting Heart Rate": 74, "Red blood cell count": 4.33}, "Omar": {...}, ...}
aus der Tabelle:
Name Birth state Gender Resting Heart Rate Red blood cell count
Florian Upper Austria Male 60 bpm 5.2 trillion cells/L
Yasmine Bayern Female 50 bpm 4.9 trillion cells/L
Max Bayern Male 74 bpm 4.33 trillion cells/L
Omar Virginia Male 78 bpm 4.67 trillion cells/L
Die Tabelle ist jetzt nicht wichtig.
Was ich nicht verstehe ist diese Zeile:
name, *patient_data = line.strip().split(",")
*patient_data = line.strip().split(",") sagt doch, dass die Variable "patient_data" = die Zeile bei jedem "," gesplittet und von allen Leerzeichen (strip()) entfernt? wie passt "name," da hinein??
im Grunde habe ich nur eine kurze spezifische Frage bezüglich einer Funktion die mir ein Dictionary erstellt.
Die Funktion:
Code: Alles auswählen
def dicts_2():
with open("data.csv") as csv_file:
health_profile = {}
keys = csv_file.readline().strip().split(",")[1:]
for line in csv_file:
name, *patient_data = line.strip().split(",")
health_profile[name] = {}
health_profile[name][keys[0]] = patient_data[0]
health_profile[name][keys[1]] = patient_data[1]
health_profile[name][keys[2]] = int(patient_data[2].split()[0])
health_profile[name][keys[3]] = float(patient_data[3].split()[0])
health_profile = {"Max": {"Birth state": "Bayern", "Gender": "Male", "Resting Heart Rate": 74, "Red blood cell count": 4.33}, "Omar": {...}, ...}
aus der Tabelle:
Name Birth state Gender Resting Heart Rate Red blood cell count
Florian Upper Austria Male 60 bpm 5.2 trillion cells/L
Yasmine Bayern Female 50 bpm 4.9 trillion cells/L
Max Bayern Male 74 bpm 4.33 trillion cells/L
Omar Virginia Male 78 bpm 4.67 trillion cells/L
Die Tabelle ist jetzt nicht wichtig.
Was ich nicht verstehe ist diese Zeile:
name, *patient_data = line.strip().split(",")
*patient_data = line.strip().split(",") sagt doch, dass die Variable "patient_data" = die Zeile bei jedem "," gesplittet und von allen Leerzeichen (strip()) entfernt? wie passt "name," da hinein??