计数器模块和字典

在上一个练习中,我编写了一个代码,打印了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

现在,我必须使用集合的模块Counter重写此代码,以计算提到每个山脉的次数。每行都包含一座山,其高度及其一部分。

我还需要添加一个字典,以记录特定范围内所有山脉的高度。我必须使用一个列表作为高度值。关键将是范围名称。每次在该范围内有一座新山峰时,都必须将高度添加到该钥匙的列表中。例如,在读取所有数据之后,mountain ['Himalayas'] == [8848、8586、8516、8485、8201、8167、8163、8126、8091、8027]。 (“喜马拉雅山”就是范围。)

输出应该是打印前两个范围并将范围名称添加到计数器。 然后,打印每个范围内山脉的平均高度。完成所有打印后,返回字典对象及其范围和山高列表。

我对Counter模块的了解很小,我对这项任务感到不知所措。 您对从哪里开始有什么建议?

这是到目前为止我得到的:

from collections import Counter
from collections import defaultdict
from statistics import mean
def mountain_ranges(filename):
    ranges = Counter()
    heights = defaultdict(list)

提前谢谢您。...

cycadwong 回答:计数器模块和字典

以下内容将打印出您想要的内容,并返回高度计数器和字典。

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

所以您知道,我个人并不认为在这里使用Counter是必要的或“正确”的处理方式,但是由于这是您的要求,所以这就是我给您的。实际上,它甚至不是在此处使用Counter的唯一方法-您可以在遍历行时创建每个范围的列表,然后仅应用Counter(list_of_ranges)但适用于较大的文件这将意味着在内存中创建一个大列表,这似乎也毫无意义。

为清楚起见,我个人不使用Counter来获取计数的解决方案将是仅使用range_heights字典并像这样对dict进行理解:

range_counts = {r: len(heights) for r,heights in range_heights.items()}

本文链接:https://www.f2er.com/2891406.html

大家都在问