9.20作业

本文深入探讨了Python中四种主要的数据结构:列表、元组、字典和集合的特点与使用方法。通过实例演示了每种数据结构的基本操作,如创建、添加、删除元素等,并对比了它们之间的区别和联系。此外,还提供了英文歌词的词频统计案例,展示了如何利用这些数据结构进行文本处理。

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

总结列表,元组,字典,集合的联系与区别:列表是以[ ]形式表示,元组是以( )表示,字典以{ }表示,集合则是以[()]的形式表示。列表和元组都是有序的,集合和字典是无序的。元组只可读,其他都可读写。字典是以键值对存储。集合中的元素不能重复,列表、元组和字典的元素均可重复。

 

列表

name = ['Linda', 'Lucy', '狗剩', '皮蛋', '狗剩']
print(name)

print(name[0])

len(name)
name.index('狗剩')
name[1]='Troy'
print(name)

name.sort()
print(name)

name.insert(1,'二蛋')
print(name)

name.append('狗屎')
print(name)

name.pop(1)
print(name)

del name[-1]
print(name)

  运行结果

 

集合

s = {1, 2, 3}
print(s)

s.add(4)
print(s)

s.remove(2)
print(s)

s = set([1, 1, 2, 2, 3, 3])
print(s)

s1 = {1, 2, 3}
s2= {2, 3, 4}
print(s1&s2)
print(s1|s2)
print(s2-s1)

  运行结果

英文歌词

英文词频统计:

  • 下载一首英文的歌词或文章str
  • 分隔出一个一个的单词 list
  • 统计每个单词出现的次数 dict
strhello='''
Hello, it's me, I was wondering
If after all these years you'd like to meet to go over everything
They say that time's supposed to heal, yeah
But I ain't done much healing

Hello, can you hear me?
I'm in California dreaming about who we used to be
When we were younger and free
I've forgotten how it felt before the world fell at our feet

There's such a difference between us
And a million miles

Hello from the other side
I must've called a thousand times 
To tell you I'm sorry, for everything that I've done
But when I call you never seem to be home

Hello from the outside
At least I can say that I've tried 
To tell you I'm sorry, for breaking your heart
But it don't matter, it clearly doesn't tear you apart anymore

Hello, how are you?
It's so typical of me to talk about myself, I'm sorry
I hope that you're well
Did you ever make it out of that town where nothing ever happened?

It's no secret
That the both of us are running out of time

So hello from the other side
I must've called a thousand times 
To tell you I'm sorry, for everything that I've done
But when I call you never seem to be home

Hello from the outside
At least I can say that I've tried 
To tell you I'm sorry, for breaking your heart
But it don't matter, it clearly doesn't tear you apart anymore

Ooh, anymore
Ooh, anymore
Ooh, anymore
Anymore...

Hello from the other side
I must've called a thousand times 
To tell you I'm sorry, for everything that I've done
But when I call you never seem to be home

Hello from the outside
At least I can say that I've tried 
To tell you I'm sorry, for breaking your heart
But it don't matter, it clearly doesn't tear you apart anymore'''

strgc = strhello.replace('.',' ')
strList = strhello.split()
print(len(strList),strList)    #分隔一个单词并统计英文单词个数
strSet = set(strList)      #将列表转化成集合,再将集合转化成字典来统计每个单词出现次数
print(strSet)
strDict={}
for hello in strSet:
    strDict[hello] =strList.count(hello)
print(strDict,len(strDict))

运行结果

转载于:https://www.cnblogs.com/fanfanfan/p/9689942.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值