第3次 组合数据类型,英语词频统计

本文详细介绍了Python中列表、元组、字典和集合的定义与操作,包括增删改查等基本功能,并通过具体示例展示了如何使用这些数据结构。此外,还提供了一个英文歌词的词频统计实例,利用Python的字符串处理和数据结构功能进行词频分析。
#定义一个列表
classmates =['lucy','Bob','amy','陈四','tom',65]
new = ['Tim','Chenjie']
#向classmates中下标为2处插入一个元素
classmates.insert(2,'Wuyifan')
#使用append向末尾增加一个元素
classmates.append('Chenjie')
classmates.extend(new)
print(classmates)
classmatesSet = set(classmates)

classmatesSet.add('Chenjie')
print(classmatesSet)
print(list('acadswet'),tuple('ababcd'))

 

 

#列表的遍历
List1=['Tim',30,'Tom',20]
print(List1)
for one in List1:
    print(one)
#元组的遍历
Tuple1=('我要当歌手!')
print(Tuple1)
for two in Tuple1:
    print(two)
#集合的遍历
Set1={4,5,6,7}
print(Set1)
for three in Set1:
    print(three)
#字典的遍历
dict1 = {'a':10,'b':20,'c':30}
print(dict1)
for four in dict1:
    print(four,dict1[four])

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

列表:有序,可做增删改查操作,用方括号[x,y,z]的方式表示

元组:有序,不可做修改操作,用小括号(x,y,z)的方式表示

字典:无序,可做增删改查操作,其中组成元素为键值对,用花括号{a:b,c:d}的方式表示

集合:无序,可由列表创建,其中元素不重复,用花括号{x,y,z}的方式表示

 

英文词频统计:

str='''If I should stay I would only be in your way

So I'll go But I know

I'll think of your every step of the way

And I will always love you Will always love you

You my darling you Bitter-sweet memories

That is all I'm taking with me

So goodbye Please don't cry We both know I'm not

What you need And I will always love you

I will always love you

I hope life treats you kind

And I hope you have all you dreamed of

And I wish you joy and happiness

But above all this Ii wish your love

And I will always love you

I will always love you I will always love you

I will always love you I will always love you

I will always love you Darling I love you

I'll always love you'''

#每个单词的第二个以后都变成小写
a=str.lower()
print(a)
#删除空格
str=str.lstrip()
print(str)
#分隔出一个一个的单词 list
print("将歌词分隔一个一个的单词为:")
strlist=str.split()
print(strlist)
#统计每个单词出现的次数 dict
print("统计每个单词出现的次数为:")
strset=set(strlist)
for word in strset:
    print(word,strlist.count(word))

 

转载于:https://www.cnblogs.com/a1234tt/p/9753047.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值