1. 读取文件 f = open('weather_info.txt').read() 出现错误

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
    

    在python2和python3中都尝试了下,python2不会弹错;这个问题是上次看到的Python3的不同之一,编码区分了unicode str和byte array, 并且默认编码不再是ascii.

    解决方法 f = open('weather_info.txt','r',encoding='utf-8').read()

  2. 形成字典

    对文档进行分行(根据换行符),每行将城市和温度根据","分隔开,按照dict[key] = value 形成字典

    发现字典中长度要比城市的总数要小,原因应当是有重名的城市。

    为找出重名城市,我想到可以建立新的字典,用计数的方式将每个城市出现的次数记录下来;如何通过重复的次数来反查城市的名字呢?

    在搜索的过程中我发现了Counter这个功能,这比自己生成字典要容易得多,而且有现成的方法

    Reference 一些Counter使用方法

    from collections import Counter
    c = Counter()
    c = Counter({'a': 4, 'b': 2})
    c = Counter(a=4, b=2)
    c = Counter("abcdefgab")
    c["a"] #访问计数值;当访问键不存在时返回0
    c.most_commen(10) #括号内为指定的top n
    # 更新计数器 (update/substract);可以使用一个iterable对象或者另一个Counter对象来更新键值
    c.update(d)
    c.substract(d)
    

    针对本次作业

    c = Counter(cities)
    c.most_commen(10)
    [('东乡', 2),
    ('东兴', 2),
    ('通州', 2),
    ('九龙', 2),
    ('河口', 2),
    ('河南', 2),
    ('朝阳', 2),
    ('宣汉', 1),
    ('无为', 1),
    ('宜城', 1)]
    

    有7个城市是重名的;

results matching ""

    No results matching ""