【发布时间】:2019-12-19 14:06:40
【问题描述】:
在之前的练习中,我编写了一个代码,用于打印 csv 文件中每座山脉的高度。你可以在这里找到它:
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
我不确定它是否理想,但它似乎有效。
这是 csv 文件的预览: mountains.csv
现在,我必须使用集合的模块计数器重写此代码,以计算每个山脉被提及的次数。每行包含一座山、它的高度和它所在的范围。
我还需要添加一个字典,记录特定范围内所有山脉的高度。我必须为高度值使用一个列表。键将是范围名称。每次在该范围内有一座新山时,都必须将高度添加到该键的列表中。例如,读取所有数据后,mountains['Himalayas'] == [8848, 8586, 8516, 8485, 8201, 8167, 8163, 8126, 8091, 8027]。 (“喜马拉雅”是范围。)
输出应该是打印前 2 个范围并将范围名称添加到计数器。 然后,打印每个范围内山脉的平均高度。在所有打印后返回包含范围及其山高列表的字典对象。
我对 Counter 模块有非常小的概念,我对这项任务感到不知所措。 您对从哪里开始有什么建议吗?
这是我目前所得到的:
from collections import Counter
from collections import defaultdict
from statistics import mean
def mountain_ranges(filename):
ranges = Counter()
heights = defaultdict(list)
提前谢谢你....
【问题讨论】:
-
您能否显示示例 CSV 和预期数据
-
你为什么“必须”使用
Counter? -
我认为这更像是一个问题解决策略的问题?想一想您将如何“手动”完成,确定工作流程中的可分组步骤(子任务),并查找某些预定义的库是否可以帮助您完成某些步骤(例如此处的 Counter)。分而治之……
-
@Alderven 我在帖子中添加了 CSV 文件预览的链接,希望对您有所帮助!
-
@PyPingu 我必须使用它,因为它是我正在关注的互联网课程中数据提取练习的要求。但是,我以前从未使用过它,而且我还处于课程的开始阶段,有点迷茫......
标签: python dictionary counter