欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python每日一练

程序员文章站 2022-03-15 22:33:35
...

前言

人生苦短,我用python【2018.6.3】

要求

统计文本文件中单词的个数

代码

# -*- coding:utf-8 -*-
import re


def main():
    fp = open("word.txt", "r")
    dictResult = {}
    for line in fp.readlines():
        li = re.findall(r"[\w-]+", line.lower(), re.I)
        for letter in li:
            dictResult[letter] = 1 + dictResult.get(letter, 0)
        # word_list = reduce(lambda x, y: x + y, li) #将多维列表 转化为一维列表
    # count total word
    print len(dictResult)
    # sort the key
    print sorted(dictResult.keys())
    # sort the value
    result = sorted(dictResult.items(), key=lambda item: item[1])  # dictResult.items()创建了可迭代的对象
    print result
    print dictResult


if __name__ == "__main__":
    main()

返回结果:

['allow', 'and', 'as', 'assets', 'bank', 'banks', 'beijing', 'by', 'central', 'china', 'chongqing', 'credit', 'economy', 'first', 'guangdong', 'high-quality', 'in', 'introduced', 'last', 'latest', 'move', 'municipalities', 'of', 'other', 'people', 'program', 'provinces', 'rated', 'refinance', 's', 'said', 'shandong', 'shanghai', 'six', 'support', 'the', 'to', 'was', 'will', 'year']
[('beijing', 1), ('shandong', 1), ('six', 1), ('people', 1), ('move', 1), ('year', 1), ('as', 1), ('program', 1), ('guangdong', 1), ('rated', 1), ('support', 1), ('shanghai', 1), ('other', 1), ('was', 1), ('economy', 1), ('municipalities', 1), ('last', 1), ('refinance', 1), ('said', 1), ('china', 1), ('central', 1), ('by', 1), ('chongqing', 1), ('introduced', 1), ('high-quality', 1), ('assets', 1), ('of', 1), ('will', 1), ('credit', 1), ('s', 1), ('allow', 1), ('banks', 1), ('first', 1), ('latest', 1), ('in', 2), ('provinces', 2), ('to', 2), ('bank', 2), ('and', 3), ('the', 5)]
{'and': 3, 'beijing': 1, 'shandong': 1, 'six': 1, 'people': 1, 'move': 1, 'year': 1, 'as': 1, 'program': 1, 'in': 2, 'guangdong': 1, 'provinces': 2, 'rated': 1, 'support': 1, 'shanghai': 1, 'to': 2, 'other': 1, 'was': 1, 'economy': 1, 'municipalities': 1, 'last': 1, 'refinance': 1, 'said': 1, 'china': 1, 'central': 1, 'by': 1, 'bank': 2, 'chongqing': 1, 'introduced': 1, 'high-quality': 1, 'assets': 1, 'of': 1, 'will': 1, 'credit': 1, 's': 1, 'allow': 1, 'banks': 1, 'the': 5, 'first': 1, 'latest': 1}

总结

学习了正则表达式的匹配和字典的用法,按key排序和按value排序,也浅尝了lambda表达式。