Python词频统计

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

def getSetOfWords(words):
    '''
    统计出文本中所有有的字符
    '''
    myset = set()
    for ch in words:
        if ch in myset:
            pass
        else:
            myset.add(ch)
    return myset

def getResult(words):
    '''
    返回统计结果(字典)
    '''
    result = {}
    myset = getSetOfWords(words)
    for ch in myset:
        result[ch]=words.count(ch)
    return result

words = input("Please input the words : ")
print getResult(words)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容