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

本文介绍Python中字典、列表、元组和集合的基本操作,并通过实例演示了如何使用这些数据结构进行学生的成绩管理和英文文本的词频统计。

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

dic={"aa":"90","bb":"91","cc":"92","dd":"94","ee":"95"}
print("查询aa的成绩")
print(dic.get('aa'))
print("删除bb成绩")
dic.pop('bb')
print("bb的成绩:","bb" in dic,"不存在")
print("aa成绩改为80")
dic['aa']=80
print(dic.items())
print("添加gg的成绩")
dic["gg"]="99"
print(dic)
print("所有学生名单:")
for i in dic:
print(i)
print("所有学生成绩:")
for i in dic:
print(i,dic[i])

查询aa的成绩
90
删除bb成绩
bb的成绩: False 不存在
aa成绩改为80
dict_items([('aa', 80), ('cc', '92'), ('dd', '94'), ('ee', '95')])
添加gg的成绩
{'aa': 80, 'cc': '92', 'dd': '94', 'ee': '95', 'gg': '99'}
所有学生名单:
aa
cc
dd
ee
gg
所有学生成绩:
aa 80
cc 92
dd 94
ee 95
gg 99

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

lis=["1","2","3","4"]
tu=("4","5")
dic={"1":"91","2":"92","3":"93","4":"94"}
s=set(lis)
print("列表")
for i in lis:
print(i)
print("元组")
for i in tu:
print(i)
print("字典")
for i in dic:
print(i,dic[i])
print("set")
for i in s:
print(i)

列表
1
2
3
4
元组
4
5
字典
1 91
2 92
3 93
4 94
set
3
2
1

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

(1)列表是任意对象的序列。列表用方括号表示。

(2)将一组值打包到一个对象中,称为元组。元组用圆括号表示。元组和列表的大部分操作相同。但是,列表是不固定的,可以随时插入,删除;而元组一旦确认就不能够再更改。所以,系统为了列表的灵活性,就需要牺牲掉一些内存;而元组就更为紧凑。

(3)与列表和元组不同,集合是无序的,也不能通过索引进行访问。此外,集合中的元素不能重复。

(4)字典就是一个关联数组或散列表,其中包含通过关键字索引的对象。用大括号表示。与集合相比,通过关键字索引,所以比集合访问方便。字典是Python解释器中最完善的数据类型。

3.英文词频统计实例

A. 待分析字符串  

B. 分解提取单词

 a. 大小写 txt.lower()  b. 分隔符'.,:;?!-_’    c.单词列表

s='''Twinkle, twinkle, little star,

How I wonder what you are.

Up above the world so high,

Like a diamond in the sky.

Twinkle, twinkle, little star,

How I wonder what you are!

When the blazing sun is gone,

When he nothing shines upon,

Then you show your little light,

Twinkle, twinkle, all the night.

Twinkle, twinkle, little star,

How I wonder what you are!

Then the traveler in the dark

Thanks you for your tiny spark;

He could not see which way to go,

If you did not twinkle so.

Twinkle, twinkle, little star,

How I wonder what you are!

Twinkle Twinkle Little Star'''
s=s.lower()
print('大写转换为小写:'+s)
for i in ',.!':
    s=s.replace(i,' ')
print('所有标点符号替换为空格:'+s)
print('单词列表及词频:')
words=s.split(' ')
dic={}
words.sort()
disc=set(words)
for i in disc:
    dic[i] = 0
for i in words:
    dic[i] = dic[i]+1
items = dic.items()
print(items)
大写转换为小写:twinkle, twinkle, little star,

how i wonder what you are.

up above the world so high,

like a diamond in the sky.

twinkle, twinkle, little star,

how i wonder what you are!

when the blazing sun is gone,

when he nothing shines upon,

then you show your little light,

twinkle, twinkle, all the night.

twinkle, twinkle, little star,

how i wonder what you are!

then the traveler in the dark

thanks you for your tiny spark;

he could not see which way to go,

if you did not twinkle so.

twinkle, twinkle, little star,

how i wonder what you are!

twinkle twinkle little star
所有标点符号替换为空格:twinkle  twinkle  little star 

how i wonder what you are 

up above the world so high 

like a diamond in the sky 

twinkle  twinkle  little star 

how i wonder what you are 

when the blazing sun is gone 

when he nothing shines upon 

then you show your little light 

twinkle  twinkle  all the night 

twinkle  twinkle  little star 

how i wonder what you are 

then the traveler in the dark

thanks you for your tiny spark;

he could not see which way to go 

if you did not twinkle so 

twinkle  twinkle  little star 

how i wonder what you are 

twinkle twinkle little star
单词列表及词频:
dict_items([('', 10), ('shines', 1), ('sky', 1), ('world', 1), ('you', 7), ('twinkle', 8), ('in', 2), ('night', 1), ('upon', 1), ('dark\n\nthanks', 1), ('for', 1), ('what', 4), ('your', 2), ('a', 1), ('\n\nhow', 4), ('so', 2), ('above', 1), ('diamond', 1), ('show', 1), ('\n\nif', 1), ('all', 1), ('could', 1), ('\n\nwhen', 2), ('\n\nup', 1), ('way', 1), ('nothing', 1), ('high', 1), ('star', 5), ('light', 1), ('spark;\n\nhe', 1), ('is', 1), ('traveler', 1), ('which', 1), ('wonder', 4), ('did', 1), ('tiny', 1), ('little', 6), ('\n\ntwinkle', 5), ('go', 1), ('blazing', 1), ('the', 6), ('he', 1), ('i', 4), ('see', 1), ('are', 4), ('not', 2), ('sun', 1), ('to', 1), ('\n\nthen', 2), ('\n\nlike', 1), ('gone', 1)])

 

转载于:https://www.cnblogs.com/00js/p/7595950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值