组合数据类型练习,英文词频统计实例上

Posted 因陀罗

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。

1.字典实例:建立学生学号成绩字典,做增删改查遍历操作。

names=["佐助","鸣人","小樱","雏田"]
scores=["98","60","80","70"]
c=dict(zip(names,scores))
#
c[\'鹿丸\']="100"
#
c[\'佐助\']="99"
#
c.pop(\'鹿丸\')

2.列表,元组,字典,集合的遍历。

#集合遍历:
s=set("564132564")
for i in s:
    print(i)

#元组遍历:
a=1,2,3,4,5
for i in a:
    print(i)

#列表遍历:
n=list("3243242")
for i in n:
    print(i)

#字典遍历:
names=["佐助","鸣人"]
scores=["98","60"]
b=dict(zip(names,scores))
for i in b:
    print(i,b[i])


总结列表,元组,字典,集合的联系与区别。

列表:是一种有序的序列,可以随时添加和删除其中的元素,没有长度限制、元素类型可以不同。

元组:和list非常相似,但是一旦初始化便不能修改。

字典:使用键-值进行存储,其中键必须为不可变的对象。

集合:值不能重复,并且是无序的。

 

3.英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

分隔符\'.,:;?!-_’

单词列表

单词计数字典

str=\'\'\'I\'ve been reading books of old
The legends and the myths
Achilles and his gold
Hercules and his gifts
Spiderman\'s control
And Batman with his fists
And clearly I don\'t see myself upon that list
She said, where\'d you wanna go?
How much you wanna risk?
I\'m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
I want something just like this
I\'ve been reading books of old
The legends and the myths
The testaments they told
The moon and its eclipse
And Superman unrolls
A suit before he lifts
But I\'m not the kind of person that it fits
She said, where\'d you wanna go?
How much you wanna risk?
I\'m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can miss
I want something just like this
I want something just like this
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Oh I want something just like this
Doo-doo-doo, doo-doo-doo
Doo-doo-doo, doo-doo
Doo-doo-doo, doo-doo-doo
Where\'d ya wanna go?
How much you wanna risk?
I\'m not looking for somebody
With some superhuman gifts
Some superhero
Some fairytale bliss
Just something I can turn to
Somebody I can kiss
I want something just like this
Oh I want something just like this
Oh I want something just like this
Oh I want something just like this\'\'\'

#将所有大写转换为小写
str=str.lower()
print(\'全部转换为小写的结果:\'+str+\'\\n\')

#将所有将所有其他做分隔符(,.?!)替换为空格
for i in \',.?!:\':
    str=str.replace(i,\' \')
print(\'其他分隔符替换为空格的结果:\'+str+\'\\n\')

#分隔出一个一个单词
str=str.split(\' \')
print(\'分隔结果为:\',str,\'\\n\')

word = set(str)
dic={}
for i in word:
    dic[i]= str.count(i)
    
str=list(dic.items())
str.sort(key=lambda x:x[1],reverse=True)
print(str,\'\\n\')
print(\'词频前10为:\')
for i in range(10):
    word,count=str[i]
    print(\'{}\\t{}\'.format(word,count))

 

以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章

组合数据类型练习,英文词频统计实例

组合数据类型练习,英文词频统计实例上

组合数据类型练习,英文词频统计实例上

组合数据类型练习,英文词频统计实例

组合数据类型练习,英文词频统计实例

组合数据类型练习,英文词频统计实例