W poprzednim ćwiczeniu napisałem kod, który wypisuje wysokość każdej góry pliku csv. Możesz go znaleźć tutaj:
import csv
def mountain_height(filename):
""" Read in a csv file of mountain names and heights.
Parse the lines and print the names and heights.
Return the data as a dictionary.
The key is the mountain and the height is the value.
"""
mountains = dict()
msg = "The height of {} is {} meters."
err_msg = "Error: File doesn't exist or is unreadable."
# TYPE YOUR CODE HERE.
try:
with open('mountains.csv', 'r') as handle:
reader = csv.reader(handle, delimiter=',')
for row in reader:
name = row[0]
height = row[1]
mountains[name] = int(height)
for name, height in mountains.items():
print("The height of {names} is {heights} meters.".format(names=name, heights=height))
except:
print("Error: Something wrong with your file location?")
return None
Nie jestem pewien, czy jest idealny, ale wydaje się, że działa.
Oto podgląd pliku csv: mountains.csv
Teraz muszę przepisać ten kod za pomocą modułu kolekcji Licznik, aby policzyć, ile razy wspomniano o każdym pasmie górskim. Każdy rząd zawiera górę, jej wysokość i zakres, którego jest częścią.
Muszę również dodać słownik, który rejestruje wszystkie wysokości gór w określonym paśmie. Muszę użyć listy dla wartości wysokości. Klucz będzie nazwą zakresu. Za każdym razem, gdy pojawia się nowa góra w paśmie, wysokość musi zostać dodana do listy dla tego klucza. Na przykład, po odczytaniu wszystkich danych, góry ['Himalaje'] == [8848, 8586, 8516, 8485, 8201, 8167, 8163, 8126, 8091, 8027]. (Zakresem są „Himalaje”).
Wyjście powinno polegać na wydrukowaniu 2 górnych zakresów i dodaniu nazwy zakresu do licznika. Następnie wydrukuj średnią wysokość gór w każdym zakresie. Po wydrukowaniu zwróć obiekt słownikowy z zakresami i ich listami wysokości gór.
Mam bardzo małe wyobrażenia o module Licznik i czuję się przytłoczony tym zadaniem. Czy masz jakieś rady, od czego zacząć?
Oto, co mam do tej pory:
from collections import Counter
from collections import defaultdict
from statistics import mean
def mountain_ranges(filename):
ranges = Counter()
heights = defaultdict(list)
Z góry dziękuję....
1 odpowiedź
Poniższe wydrukuje to, o co prosiłeś, i zwróci licznik i słownik wysokości.
import csv
from collections import defaultdict, Counter
from statistics import mean
def mountain_height(filename):
""" Read in a csv file of mountain names and heights.
Parse the lines and print the names and heights.
Return the data as a dictionary.
The key is the mountain and the height is the value.
"""
range_heights = defaultdict(list)
range_count = Counter()
# TYPE YOUR CODE HERE.
try:
with open(filename, 'r') as handle:
reader = csv.reader(handle, delimiter=',')
for row in reader:
if row:
name = row[0]
height = row[1]
mrange = row[2]
range_heights[mrange].append(int(height))
range_count[mrange] += 1
except:
print("Error: Something wrong with your file location?")
return None
print("The 2 most frequent ranges are:")
for mrange in range_count.most_common(2):
print(f"{mrange[0]} has {mrange[1]} mountains")
print("The average heights of each range are:")
for mrange, heights in range_heights.items():
print(f"{mrange} -- {mean(heights)}m")
return range_count, range_heights
counts, heights = mountain_height('mountains.csv')
The 2 most frequent ranges are:
Himalayas has 10 mountains
Karakoram has 4 mountains
The average heights of each range are:
Himalayas -- 8321m
Karakoram -- 8194.25m
Więc wiesz, osobiście nie wierzę, że używanie Counter
tutaj jest konieczne lub „właściwy” sposób robienia rzeczy, ale ponieważ tego potrzebujesz, to właśnie ci dałem. W rzeczywistości nie jest to nawet jedyny sposób, w jaki możesz użyć Counter
tutaj - możesz utworzyć listę każdego zakresu podczas przeglądania wierszy, a następnie po prostu zastosować Counter(list_of_ranges)
, ale dla większych plików oznaczałoby to tworzenie dużej listy w pamięci, która znowu wydaje się bezcelowa.
Dla jasności moim osobistym rozwiązaniem na obliczanie wyników bez Counter
byłoby po prostu użycie słownika range_heights
i dyktowanie ze zrozumieniem w następujący sposób:
range_counts = {r: len(heights) for r, heights in range_heights.items()}
Counter
, ale czułem się miłosierny, więc po prostu wstawiłem przykład z dokumentacji do kodu OPs.
Podobne pytania
Nowe pytania
python
Python to wielozadaniowy, wielozadaniowy język programowania dynamicznie typowany. Został zaprojektowany tak, aby był szybki do nauczenia się, zrozumienia i użycia oraz wymuszania czystej i jednolitej składni. Należy pamiętać, że Python 2 oficjalnie nie jest obsługiwany od 01-01-2020. Mimo to, w przypadku pytań Pythona specyficznych dla wersji, dodaj znacznik [python-2.7] lub [python-3.x]. Korzystając z wariantu Pythona (np. Jython, PyPy) lub biblioteki (np. Pandas i NumPy), należy umieścić go w tagach.
Counter
?