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

本文通过实例展示了如何使用Python进行数据结构操作,包括字典的增删改查及遍历,并通过一个英文歌词的词频统计实例,介绍了如何进行文本处理与统计。

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

d={'01':'80','02':'70','03':'76','04':'86','05':'82'}
print(d.keys())
print(d.values())
d['08']=98
d['03']=99
d.pop('02')
print(d.get('07'))
print(d)

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

str=list('123112312')
tu=tuple('abcdrfghj')
s=set([5,4,3,1,6])
d=dict(zip(tu,str))
print(str)
print(tu)
print(s)
print(d)
for i in str:
print(i)
for j in tu:
print(j)
for h in s:
print(h)
for k in d:
print(k,d[k])

 

 

英文词频统计实例

待分析字符串

分解提取单词

大小写 txt.lower()

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

单词列表

单词计数字典

 

new='''After the Winter - Lenka

When the rain is pourin' down

"And there are snowflakes on your cheeks"

When your heart is frozen over

And there is a sea lost sun in weeks

Just remember,

Just remember,

After the winter comes the spring

That is when the blue birds

Starts to sing

And you can always count on this

After the winter comes the spring

When the trees have lost the color

And the sky is full of fears

When you feel you are going under

And your eyes are full of tears

When the bells are all hiding

And you are hiding too

Oh, darling just remember

That everything will soon be new

After the winter comes the spring

That is when the blue birds

Start to use their wings

And you can always count on this

After the winter comes the spring

Just remember,

Just remember,

Just remember,

Just remember,

After the winter comes the spring.

That is when the blue birds.

Starts to sing.

And you can always count on this

After the winter comes the spring

After the winter comes the spring'''

new = new.lower()
for i in ',."':
    new = new.replace(i,' ')
new = new.split(' ')
words = set(new)
d = {}
for i in words:
    d[i] = new.count(i)

for i in d:
    print('{0:<15}{1}'.format(i,d[i]))

 

 

转载于:https://www.cnblogs.com/1257-/p/7573369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值