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

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

dict={'01':90,'02':85,'03':85,'04':75}
print('学生学号成绩:',dict)
dict['05']=80
print('增加学号为05的学生的成绩为80,即',dict)
dict.pop('03')
print('删除学号为03的学生成绩',dict)

print('查找学号为03的学生成绩',dict.get('03'))
print('查找06的学生成绩',dict.get('06','没有该学生'))
dict['04']=70
print('修改学号为04的学生成绩为70',dict)

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

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

 

nums=('123456789')
words=('abcdefg')
ls=list('abcdefg')
s=set(ls)
tu=tuple('abcdefg')
di=dict(zip(nums,words))
print(di)
print('打印列表')
for i in ls:
    print(i)
print('打印字典')
for j in di:
     print(j,di[j])
print('打印元组')
for i in tu:
    print(i)
print('打印集合')
for i in s:
    print(i)

 

       联系与区别 : 列表是可变对象,它支持在原处修改的操作.也可以通过指定的索引和分片获取元素,区别于元组,可动态增加,删除,更新;元组和列表在结构上没有什么区别,但元组是只读的,不能修改。元组用“()”表示,元组一旦定义其长度和内容都是固定的,即不能对元组进行更新、增加、删除操作;集合内的元素没有重复的元素,通过一个set函数转换成集合,是一个无序不重复元素集;字典存储键值对数据,字典最外面用大括号,每一组用冒号连起来,然后各组用逗号隔开,字典最大的价值是查询,通过键,查找值,字典和集合都是无序的。

 

3.英文词频统计实例

    (1)待分析字符串

    (2)分解提取单词

           a. 大小写 txt.lower()

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

           c.单词列表

    (3)单词计数字典

str='''Waking up I see that everything is okay
The first time in my life and now it's so great
Slowing down I look around and I am so amazed
I think about the little things that make life great
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant, I hope that it will stay
This moment is perfect, please don't go away
I need you now
And I'll hold on to it, don't you let it pass you by
I found a place so safe, not a single tear
The first time in my life and now it's so clear
Feel calm, I belong, I'm so happy here
It's so strong and now I let myself be sincere
I wouldn't change a thing about it
This is the best feeling
This innocence is brilliant, I hope that it will stay
This moment is perfect, please don't go away
I need you now
And I'll hold on to it, don't you let it pass you by
It's the state of bliss you think you're dreaming
It's the happiness inside that you're feeling
It's so beautiful, it makes you wanna cry'''
print('待分析字符串:\n',str)

for i in ',.?!\n':
    str=str.replace(i,' ')
str=str.lower()
str=str.split(" ")
words=set(str)
dt={}
dt['i']=str.count('i')
dt['so']=str.count('so')
dt['innocence']=str.count('innocence')
dt['you']=str.count('you')
print('单词计数字典:')
for j in words:
    dt[j]=str.count(j)
for j in dt:
    print("{0:<11}{1}".format(j,dt[j]))

 

转载于:https://www.cnblogs.com/yangjlee/p/7595980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值