zoukankan      html  css  js  c++  java
  • 复合数据类型,英文词频统计

    一、列表,元组,字典,集合分别如何增删改查及遍历

    1、列表

    1)增

    • append()方法:在列表的末尾增加一个元素
    • insert()方法:在列表指定的位置上增加一个元素
    • extend()方法:可迭代,分解成元素添加在末尾

    2)删

    • pop()方法: 按照下标索引删除指定的值
    • remove()方法:按元素删除指定的值
    • del:删除列表、也可以进行切片删除

    2、元组

    • 元组不允许修改以及单个删除元素

    3、字典

    1)增

    • dict[key]=value 通过赋值的方法增加元素

    2)删

    • del dict[key]  删除单一元素,通过key来指定删除
    • dict.pop(key)  删除单一元素,通过key来指定删除

    3)改

    • dict[key]=value  通过对已有的key重新赋值的方法修改

    4)查

    • dict[key]  通过key访问value

    4、集合

    1)增

    • add()方法:增加一个元素

    2)删

    • remove()pop()方法:删除集合中一个元素

    二、总结列表,元组,字典,集合的联系与区别。参考以下几个方面:

    • 括号
    • 有序无序
    • 可变不可变
    • 重复不可重复
    • 存储与查找方式

    联系与区别如下:

    1、列表的括号是"[ ]" ,元组的括号是”( )“,字典的括号和集合的括号都是”{ }“

    2、列表与元组都为有序序列,字典与集合为无序序列;

    3、列表、字典、集合属于可变序列,而元组属于不可变序列;

    4、列表和元组允许重复,而字典和集合不允许重复;

    5

    列表以值的方式存储为值,可通过索引查找;

    元组以值的方式存储为值,可通过索引查找;

    字典以键值对的方式存储为值,一般通过键查找;

    集合以值的方式存储为值,可以通过set()来将序列和字典转换为集合。

    三、词频统计

    • 1.下载一长篇小说,存成utf-8编码的文本文件 file
    • 2.通过文件读取字符串 str
    • 3.对文本进行预处理
    • 4.分解提取单词 list
    • 5.单词计数字典 set , dict
    • 6.按词频排序 list.sort(key=lambda),turple
    • 7.排除语法型词汇,代词、冠词、连词等无语义词
      • 自定义停用词表
      • 或用stops.txt
      • 8.输出TOP(20)
      • 9.可视化:词云

     排序好的单词列表word保存成csv文件

    import pandas as pd
    pd.DataFrame(data=word).to_csv('big.csv',encoding='utf-8')

    线上工具生成词云:
    https://wordart.com/create 

    词频统计代码如下:

    import pandas as pd
    
    mum = {'it', 'if', 'the', 'at', 'for', 'on', 'and', 'in', 'to', 'of', 'a', 'was', 'be', 'were', 'in', 'about',
            'from', 'with', 'without', 'an', 'one', 'another', 'others', 'that', 'they', 'himself', 'itself',
             'themselves', 'if', 'when', 'before', 'though', 'although',
           'while', 'as', 'as long as', 'i', 'he', 'him', 'she', 'out', 'is', 's', 'no', 'not', 'you', 'me', 'his',
           'but'}
    
    def getsong():
     file=open("F://xiaoshuo.txt", "r")
     song=file.read().lower()
    
     ch=",.!()"
     for c in ch:
        song = song.replace(c,'')
        return song
    
    wordlist=getsong().split()
    
    wordset=set(wordlist) - mum
    wordict={}
    for w in wordset:
       wordict[w] = wordlist.count(w)
    
    wordsort=list(wordict.items())
    wordsort.sort(key= lambda x:x[1],reverse=True)
    
    '''输出top20'''
    for i in range(20):
        print(wordsort[i])
    
    '''保存为csv文件'''
    pd.DataFrame(data=wordsort).to_csv(r'F:	est.csv',encoding='utf-8')

    下面是xiaoshuo.txt

     

    运行截图如下:

    控制台输出:

     

    可视化词云:

     

  • 相关阅读:
    kubernetes中跨namespace的服务调用 & 外部服务调用 & host配置
    pm2 ——带有负载均衡功能的Node应用的进程管理器
    Linux——docker启用配置:dockerfile——ubuntu
    .net core 单元测试——sonar
    Linux——工具参考篇(3)——ps 进程查看器
    开发框架
    Django框架 + Djiango安装 + First Djiango + 常用命令
    计算机网络知识点随机整理
    Ubuntu tricks
    Python 图片Resize.py
  • 原文地址:https://www.cnblogs.com/yuanzhenpeng/p/10593287.html
Copyright © 2011-2022 走看看