复合数据类型,英文词频统计

作业要求来自于 https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753

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

  • 列表

1.增加 

list.append(obj)

增加元素到末尾

LOL = ['阿达', '阿萨德']
LOL.append('AA')
print(LOL)

 

list.insert(index, obj)

增加元素到指定位置

LOL = ['s是', '阿萨德']
LOL.insert(1, '爱上')
print(LOL)

 

list.extend(list2)

将list2列表中的元素增加到list中

LOL = ['AA', '宝贝']
YXLM=['存储', '等待']
LOL.extend(YXLM)
print(LOL)

 

2.删除

list.pop(index)

删除指定位置的元素

LOL = ['吃v', '存储', '这种']
LOL.pop()
print(LOL)

 

list.pop()

删除最后一个元素

LOL = ['吉格斯', '泽拉斯', '杰斯']
LOL.pop(1) 
print(LOL)

 

 

3.改

LOL = ['AA', '宝贝', '是是是']
LOL[2]='存储'
print(LOL)

 

 

4.查找

通过切片方式取值
1
2
LOL = ['吉格斯''泽拉斯''杰斯''塞拉斯']
print(LOL[0:2])

 

取下标后面所有的值
1
2
LOL = ['吉格斯''泽拉斯''杰斯''塞拉斯']
print(LOL[1:])

 

 

 

取下标前面所有的值
1
2
LOL = ['吉格斯''泽拉斯''杰斯''塞拉斯']
print(LOL[:3])

  

 

取所有的值
1
2
LOL = ['吉格斯''泽拉斯''杰斯''塞拉斯']
print(LOL[:])

  

 

按位置取值

1
2
LOL = ['吉格斯''泽拉斯''杰斯''塞拉斯']
print(LOL[0])

  

 

 5.遍历

list=['迪丽热巴','古力娜扎','马尔扎哈']
for l in list:
print(l)

 

 

 

 

  • 元组
  •  另一种有序列表叫元组:tuple。tuple和list非常类似,但是tuple一旦初始化就不能修改
  1. 增加
    tuple1=('维恩', '大嘴', '奎因')
    tuple2=('璐璐', '奶妈', '风女')
    tuple=tuple1+tuple2
    print(tuple)

     

  2. 删除
    tuple1=('维恩', '大嘴', '奎因')
    print(tuple1)
    del tuple1
    print(tuple1)

     

  3. tuple1=('维恩', '大嘴', '奎因')
    tuple1=list(tuple1)
    tuple1[2]='卢锡安'
    print(tuple1)

     

  4. 查找

       通过下标索引,从0开始

tuple1=('维恩', '大嘴', '奎因') print(tuple1[1])

 

 

      切片,顾头不顾尾

1
2
tuple1=('维恩''大嘴''奎因')
print(tuple1[:2])

 

   5.遍历

tuple=('迪丽热巴','古力娜扎','马尔扎哈')
for t in tuple:
    print(t)

 

 

  • 字典

1.增加

dict={'盲僧':6300,'寒冰':450}
dict["疾风剑豪"]=100

print(dict)

 

 

2.删除

 

1
2
3
dict={'盲僧':6300,'寒冰':450}
del dict['寒冰']
print(dict)

 

 

1
2
3
dict={'盲僧':225,'寒冰':450}
dict.pop('寒冰')
print(dict)

 

 

 

3.改

dict={'盲僧':6300,'寒冰':450}
dict["寒冰"]=3150
print(dict)

 

 

4.查找

 通过key访问value值

1
2
dict={'盲僧':6300,'寒冰':450}
print(dict['寒冰'])

  

 

以列表返回可遍历的(键, 值) 元组数组

1
2
dict={'盲僧':6300,'寒冰':450}
print(dict.items())

  

 

 5.遍历

dict={'陈泽诚'':21','迪丽热巴'':27'}
for d in dict:
    print(d)

 

 

 

 

  • 集合
  1. 增加
    s=set(['疾风剑豪','盲僧'])
    s.add('诺克萨斯之手')
    print(s)

     

     

     

  2. 删除
    s=set(['疾风剑豪','盲僧'])
    s.remove('盲僧')
    print(s)

     

     

     

  3. s=set(['one', 'two'])
    s=list(s)
    s[1]= 'three'
    s=set(s)
    print(s)

     

    
    
  4. 遍历
    s=set(['测试', 'key'])
    for x in s:
        print(x)

     

     

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

(1)列表

  • 列表list,用中括号“[ ]”表示
  • 列表是一组任意类型的值,按照一定顺序组合而成的 
  • 可以随时添加删除修改其中的元素
  • 元素可重复
  • 存储时每一个元素被标识一个索引,每当引用时,会将这个引用指向一个对象,所以程序只需处理对象的操作

(2)元组

  • 元组tuple,用小括号“( )”表示
  • 与列表相同,有序
  • 一旦初始化就不能修改
  • 元素可重复
  • 与列表相似,元组是对象引用的数组

(3)字典

  • 字典dict,用大括号“{key,value}”表示
  • 字典中的项没有特定顺序,以“键”为象征 
  • 因为是无序,故不能进行序列操作,但可以在远处修改,通过键映射到值
  • key不能重复
  • 字典存储的是对象引用,不是拷贝,和列表一样

(4)集合

  • 集合set,用小括号“( )”表示
  • 无序
  • 可变,可以添加和删除元素
  • 无重复
  • 与列表相似

 

3.词频统计

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pandas as pd
file = open('cipher.txt''r', encoding='utf8')
 
# 排除词汇列表
exclude = {'a','an','and','was','as','up','my','it','here','at','ll','his','s','he','that','from','had','have','we','the','i','you','in','on','but','with','not','by','its','for','of','to'}
 
# 对文本预处理
def getfile():
    sep = "'?', '?', '!'," '", "'", ' "', '"',':',':','.',','',''.''。','“','”',','"
    text =file.read().lower()
    for ii in sep:
        text = text.replace(ii, ' ')
    return text
 
#提取单词,单词计数词典
fileList = getfile().split()
fileDict = set(fileList)
exclude1 = set(exclude)
fileDict = fileDict-exclude1
 
# 统计单词数量
countDict = {}
for word in fileDict:
    countDict[word] = fileList.count(word)
print(countDict.items())
word = list(countDict.items())
 
# 排序单词数量
word.sort(key=lambda x: x[1], reverse=True)
print(word)
 
# 输出TOP20
for in range(20):
    print(word[i])
pd.DataFrame(data=word).to_csv('yyy.csv', encoding='utf-8')

  

  • 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

 

posted on 2019-03-22 15:04  liliguang  阅读(180)  评论(0编辑  收藏  举报