- 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
song='''When I was young I had listen to the radio
Waiting for my favorite songs
When they played I'd sing along,
It make me smile.
Those were such happy times and not so long ago
How I wondered where they had gone.
But they are back again just like a long lost friend
All the songs I love so well.
Every shalala every
still shines.
Every shing-a-ling-a-ling that they are starting to sing
so fine'''
song=song.lower()
song=song.replace(',',' ')
song=song.replace('.',' ')
song=song.replace('-',' ')
print('long出现的次数:',song.count('long'))
print(' I 出现的次数:',song.count('I'))
newsong=song.split(' ')
print(' 改变后的英文歌曲:',newsong) - 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等
s=list('211321323123121233')
s.append('1')
s.insert(2,'2')
s.pop()
s.pop(7)
print(s)
print('3分的起始位置是',s.index('3'))
print('1分的有',s.count('1'),'个')
print('3分的有',s.count('3'),'个') - 简要描述列表与元组的异同。
list是一种有序的序列,正向递增、反向递减序列,可以随时添加和删除其中的元素;没有长度限制、元素类型可以不同;
tuple可读取里面的元素,但是不能改变其中的元素。