1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
song='''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?
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'''
song=song.lower()
print(song)
for i in song:
song1=song.replace(',',' ')
song2=song1.replace('?',' ')
print(song2)
songs=song2.split(' ')
print(songs)
print(songs.count('hello'))
print(songs.count('you'))
2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
grade=list('13231313212212312')
grade.append('3')
grade.pop()
grade[3]='1'
print(grade[5])
for i in range (len(grade)):
grade[i]=int(grade[i])
for i in range (len(grade)):
if grade[i]==2:
print(i)
break
print('1分的同学有{0}个'.format(grade.count(1)))
print('3分的同学有{0}个'.format(grade.count(3)))
3、简要描述列表与元组的异同。
元组和列表十分类似,只不过元组和字符串一样是不可变的即你不能修改元组;而列表是可以修改的。