9.20作业

本文详细介绍了Python中列表、元组、字典和集合的基本概念与操作,包括它们的创建、特性及遍历方式,并通过一个英文歌词的词频统计实例展示了如何运用这些数据结构解决实际问题。

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

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

列表:

可以用list()函数或者方括号[]创建,元素之间用逗号’,‘’分隔。 

列表的元素不需要具有相同的类型 

可以进行增删改查操作

元祖:

可以用tuple()函数或者方括号()创建,元素之间用逗号’,‘’分隔。 

元组的元素不需要具有相同的类型 

元素的值一旦创建就不可修改

字典:

元素由键(key)和值(value)组成 

可以用dict()函数或者方括号()创建,元素之间用逗号’,‘’分隔,键与值之间用冒号”:”隔开 

键必须是唯一的,但值则不必。值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组 

使用键(key)来访问元素

集合:

可以用set()函数或者方括号{}创建,元素之间用逗号”,”分隔。 

与字典相比少了键  

不可以有重复元素

 

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

 列表:

classmate=['Michael','Bob','luck','angle','李斯',55]
new=['nance']
classmate[0]
classmate[-1]
classmate[0:3]
classmate.insert(4,'gfds')
classmate.append('fe')
classmate.extend(new)
classmate.pop()
del classmate[0]
#   print(classmate)
for stu in classmate:
    print(stu)

 

元组:

tuple=(("apple","banana"),("grape","orange"),("watermelon",),("grapefruit",))
for i in range(len(tuple)) :
   print(tuple[i],"")

字典和集合:

st = set('集合的遍历')        #集合的遍历
print(st)
for b in st:
    print(b)

dt = {'a':80,'b':79,'c':90}        #字典的遍历
print(dt)
for i in dt:
    print(i,dt[i])

 

 

 

 

 

 

 

 

 

 

(3)英文词频统计:

str ='''There's a girl but I let her get away 
It's all my fault cause pride got in the way 
And I'd be lying if I said I was OK 
About that girl the one I let get away 
I keep saying no 
This can't be the way we're supposed to be 
I keep saying no 
There's gotta be a way to get you close to me 
Now I know you gotta 
Speak up if you want somebody 
Can't let him get away oh no 
You don't wanna end up sorry The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
There's a girl but I let her get away 
It's my fault cause I said I needed space 
I've been torturing myself night and day 
About that girl the one I let get away 
I keep saying no 
This can't be the way we're supposed to be 
I keep saying no 
There's gotta be a way to get you 
Gotta be a way 
To get you close to me 
You gotta 
Speak up if you want somebody 
Can't let him get away oh no 
You don't wanna end up sorry 
The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
No home for me 
No home cause I'm broken 
No room to breathe 
And I got no one to blame 
No home for me 
No home cause I'm broken 
About that girl 
The one I let get away 
So you better 
Speak up if you want somebody 
You can't let him get away no no 
You don't wanna end up sorry 
The way that I'm feeling everyday 
Don't you know 
No no no no 
There's no home for the broken heart 
Don't you know 
No no no no 
There's no home for the broken 
Oh 
You don't wanna lose at love 
It's only gonna hurt too much 
I'm telling you 
You don't wanna lose at love 
It's only gonna hurt too much 
I'm telling you 
You don't wanna lose at love 
Cause there's no home for the broken heart 
That girl 
The one I let get away 
'''
daxie = str.upper()
jishu = str.count('no')
print(str.split(' '))
print(daxie)
print(jishu)

 

 

 

 

统计每个单词出现的次数 dict

转载于:https://www.cnblogs.com/zhangjij/p/9680011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值