• Python-统计值计算、词频统计


    一、统计值计算

    def getNum():  # 获取用户不定长度的输入
        nums = []
        iNumStr = input("请输入数字(回车退出): ")
        while iNumStr != "":
            nums.append(eval(iNumStr))
            iNumStr = input("请输入数字(回车退出): ")
        return nums
    
    
    def mean(numbers):  # 计算平均值
        s = 0.0
        for num in numbers:#遍历numbers
            s = s + num
        return s / len(numbers)
    
    
    def dev(numbers, mean):  # 计算方差
        sdev = 0.0
        for num in numbers:
            sdev = sdev + (num - mean) ** 2
        return pow(sdev / (len(numbers) - 1), 0.5)
    
    
    def median(numbers):  # 计算中位数
        sorted(numbers)
        size = len(numbers)
        if size % 2 == 0:
            med = (numbers[size // 2 - 1] + numbers[size // 2]) / 2
        else:
            med = numbers[size // 2]
        return med
    
    n = getNum()  # 主体函数
    m = mean(n)
    print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n, m), median(n)))
    统计值计算

     二、词频统计

    def getText():
        txt = open("Text.txt", "r").read()
        txt = txt.lower()#字母变小写
        for ch in '!"#$%&()*+,-./:;<=>?@[\]^_‘{|}~':
            txt = txt.replace(ch, " ")  # 将文本中特殊字符替换为空格
        return txt
    
    
    hamletTxt = getText()
    words = hamletTxt.split()
    counts = {}
    for word in words:
        counts[word] = counts.get(word, 0) + 1
    items = list(counts.items())
    items.sort(key=lambda x: x[1], reverse=True)
    for i in range(10):
        word, count = items[i]
        print("{0:<10}{1:>5}".format(word, count))
    词频统计

  • 相关阅读:
    [Next] 六.next的优化
    [Next] 五.next自定义内容
    Mac解决端口占用
    [Next] 四.在next中引入redux
    [Next] 服务端渲染知识补充
    [Next] 三.next自定义服务器和路由
    哪些使用UDP、TCP、IP协议
    IDEA配置git
    ssm整合配置
    git传输远程仓库
  • 原文地址:https://www.cnblogs.com/MoooJL/p/12542406.html
Copyright © 2020-2023  润新知