组合数据类型练习,英文词频统计实例上
Posted wsxhj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了组合数据类型练习,英文词频统计实例上相关的知识,希望对你有一定的参考价值。
1.实例:建立学生学号成绩字典,做增删改查遍历操作。
>>> d={\'01\':\'95\',\'02\':\'98\',\'03\':\'80\',\'04\':\'86\'}
>>> d[\'01\'] 查询
\'95\'
>>> d.pop(\'03\') 删除
\'80\'
>>> d
{\'01\': \'95\', \'02\': \'98\', \'04\': \'86\'}
>>> d[\'05\']=\'99\' 添加
>>> d
{\'01\': \'95\', \'02\': \'98\', \'04\': \'86\', \'05\': \'99\'}
>>> d[\'03\']=\'100\' 修改
>>> d
{\'01\': \'95\', \'02\': \'98\', \'04\': \'86\', \'05\': \'99\', \'03\': \'100\'}
>>>
结果如下:
2.列表,元组,字典,集合的遍历。
ls=list(\'2122321223\')
>>> ls
[\'2\', \'1\', \'2\', \'2\', \'3\', \'2\', \'1\', \'2\', \'2\', \'3\']
>>> tu=tuple(\'abcdefghij\')
>>> tu
(\'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'i\', \'j\')
>>> c=set(\'250\')
>>> c
{\'5\', \'2\', \'0\'}
>>> for i in ls:
print(i)
2
1
2
2
3
2
1
2
2
3
>>> for i in tu:
print(i)
a
b
c
d
e
f
g
h
i
j
>>> for i in c:
print(i)
5
2
0
>>>
结果如下:
3.总结列表,元组,字典,集合的联系与区别
列表list是任意对象的序列,列表中的元素用方括号表示。
元组和列表十分类似,只不过元组和字符串一样是不可变的,即你不能修改元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改
列表是任意对象的有序集合,列表中的元素都是可变的,这是不同于元组的。
字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。
4.英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符\'.,:;?!-_’
- 单词列表
- 单词计数字典
big=\'\'\'Oooh
Oooh
Put your make-up on
Get your nails done
Curl your hair
Run the extra mile
Keep it slim so they like you, do they like you?
Get your sexy on
Don\'t be shy, girl
Take it off
This is what you want, to belong, so they like you
Do you like you?
You don\'t have to try so hard
You don\'t have to, give it all away
You just have to get up, get up, get up, get up
You don\'t have to change a single thing
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try
Yooou don\'t have to try
Oooh
Oooh
Get your shopping on, at the mall, max your credit cards
You don\'t have to choose, buy it all, so they like you
Do they like you?
Wait a second,
Why, should you care, what they think of you
When you\'re all alone, by yourself, do you like you?
Do you like you?
You don\'t have to try so hard
You don\'t have to, give it all away
You just have to get up, get up, get up, get up
You don\'t have to change a single thing
You don\'t have to try so hard
You don\'t have to bend until you break
You just have to get up, get up, get up, get up
You don\'t have to change a single thing
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try
Yooou don\'t have to try
Oooh
Oooh
You don\'t have to try so hard
You don\'t have to, give it all away
You just have to get up, get up, get up, get up
You don\'t have to change a single thing
You don\'t have to try, try, try, try
You don\'t have to try, try, try, try
You don\'t have to try
You don\'t have to try
Take your make-up off
Let your hair down
Take a breath
Look into the mirror, at yourself
Don\'t you like you?
Cause I like you\'\'\'
big=big.lower()
for i in \',.?!\':
big.replace(i,\'\')
words=big.split(\' \')
print(words)
print(\'try :\',big.count(\'try\'))
print(\'you :\',big.count(\'You\'))
以上是关于组合数据类型练习,英文词频统计实例上的主要内容,如果未能解决你的问题,请参考以下文章