1、字典实例:建立学生学号成绩字典,做增删改查遍历操作。
d=['1','2','3','4','5'] s=[87,91,78,90,82] g=dict(zip(d,s)) g['12']=90 print('增加一个学生:',g) g.pop('3') print('删除一个学生:',g) g['4']=70 print('修改一个学生:',g) print('查找2学生分数:',g['2']) print('查询字典里面没有的学生:',g.get('7','找不到该学生'))

2、列表,元组,字典,集合的遍历。总计列表,元组,字典,集合的联系与区别。
d={'001':'89','002':'94','003':'92','004':'87','005':'88','006':'86','007':'83'}
list=['01','02','03','04','05','06','07']
tuple=('4','2','6','7','3','7','9',)
s={'83874950'}
for i in d:
print(i,d[i])
for i in list:
print(i)
for i in tuple:
print(i)
for i in s:
print(i)


3、英文词频统计实例
- 待分析字符串
- 分解提取单词
- 大小写 txt.lower()
- 分隔符'.,:;?!-_’
- 单词列表
- 单词计数字典
news='''I'm tired of being what you want me to be Feeling so faithless lost under the surface Don't know what you're expecting of me Put under the pressure of walking in your shoes (Caught in the undertow just caught in the undertow) Every step I take is another mistake to you (Caught in the undertow just caught in the undertow) I've become so numb I can't feel you there I've become so tired so much more aware I've becoming this all I want to do Is be more like me and be less like you Can't you see that you're smothering me Holding too tightly afraid to lose control Cause everything that you thought I would be Has fallen apart right in front of you (Caught in the undertow just caught in the undertow) Every step that I take is another mistake to you (Caught in the undertow just caught in the undertow) And every second I waste is more than I can take I've become so numb I can't feel you there I've become so tired so much more aware I've becoming this all I want to do Is be more like me and be less like you And I know I may end up failing too But I know You were just like me with someone disappointed in you I've become so numb I can't feel you there I've become so tired so much more aware I've becoming this all I want to do Is be more like me and be less like you I've become so numb I can't feel you there Is everything what you want me to be I've become so numb I can't feel you there Is everything what you want me to be ''' news=news.lower() print(news) for i in ',.': news=news.replace(i,'') words=news.split(' ') print(words) dic={} for i in words: dic[i]=news.count(i) news=list(dic.items()) print('单词计数字典:',news,'/n')

本文通过Python实例展示了字典、列表、元组、集合的使用与遍历,对比了它们之间的联系与区别,并提供了学生学号成绩字典的操作案例。此外,还详细介绍了如何利用Python进行英文文本的词频统计,包括文本预处理、单词提取与计数等步骤。
1953

被折叠的 条评论
为什么被折叠?



