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

本文介绍了使用Python进行数据结构操作的实例,包括字典的基本操作如增删改查及遍历,同时对比了列表、元组、字典和集合的特点与应用场景,并提供了一个英文词频统计的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

garden={'01':60,'02':75,'03':83,'04':90}
#增
garden['05']=92
#删
garden.pop('01')
#改
garden['02']=70
#查
garden.get('03')
print(garden)

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

l = list('123456')
t = tuple('654321')
d = {'小明':60,'小红':75,'小刚':95}
s = set([1,2,3,4,2,3,4])
for i in l:
print(i)
for i in t:
print(i)
for i in d:
print(d[i])
for i in s:
print(i)

联系与区别:

1.列表和元组都是有序的,而字典和集合是无序的

2.元组和列表很相似,但列表可以修改,元组不可修改

3.列表用“[]”表示。列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素。

4.元组用“()”表示。元组是只读的,不能修改。元组一旦定义其长度和内容都是固定的。

5.字典用“{}”表示。字典存储键值对(key-value)数据。每一组用冒号连起来,然后各组用逗号隔开。

6.集合没有特殊的表示方法,而是通过一个set函数转换成集合。集合内的元素没有重复的元素,主要是消除重复元素。

  三.英文词频统计实例

  1. 待分析字符串
  2. 分解提取单词
    1. 大小写 txt.lower()
    2. 分隔符'.,:;?!-_’
    3. 单词列表
  3. 单词计数字典

word='''I'm a big big girl

in a big big world

It's not a big big thing

if you leave me

but I do do feel that

I too too will miss you much

miss you much...

 

I can see the first leaf falling

it's all yellow and nice

It's so very cold outside

like the way I'm feeling inside

I'm a big big girl

in a big big world

It's not a big big thing

if you leave me

but I do do feel that

I too too will

miss you much

miss you much...

 

Outside it's now raining

and tears are falling from my eyes

why did it have to happen

why did it all have to end

I'm a big big girl

in a big big world

It's not a big big thing

if you leave me

but I do do feel

that I too too will

miss you much

miss you much

I have your arms around me

ooooh like fire

but when I open my eyes

you're gone...

I'm a big big girl

in a big big world

It's not a big big thing

if you leave me

but I do do feel

that I too too will

miss you much

miss you much...

I'm a big big girl

in a big big world

It's not a big big thing

if you leave me

but I do feel I will

miss you much

miss you much...'''
word=word.lower()
word=word.replace('\n',' ')
word=word.replace("'",' ')
words=word.split(' ')
dic={}
keys=set(words)
for i in keys:
dic[i]=word.count(i)
print(dic)

 

 

转载于:https://www.cnblogs.com/951111ldj/p/7577312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值