- 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
s='''It's been a long day, without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
Damn who knew all the planes we flew
Good things we've been through
That I'll be standing right here
Talking to you about another path
I know we loved to hit the road and laugh
But something told me that it wouldn't last
Had to switch up look at things different see the bigger picture
Those were the days hard work forever pays
Now I see you in a better place
How could we not talk about family when family's all that we got?
Everything I went through you were standing there by my side
And now you gonna be with me for the last ride
It's been a long day without you my friend
And I'll tell you all about it when I see you again
We've come a long way from where we began
Oh I'll tell you all about it when I see you again
When I see you again
'''
#转换为小写
a=s.lower()
#将,和?替换为空格
a=a.replace(',',' ')
a=a.replace('?',' ')
#分隔出一个一个的单词
b=a.split(' ')
print(b)
#统计when出现的次数
b.count('when')
b.count('see')
b.count('i')
b.count('to') - 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
s=['1','2','3','2','1','3']
print(s)
#添加一个分数
s.append('2')
print(s)
#弹出最后一个元素
s.pop()
print(s)
#替换
s[0]='2'
print(s)
#下标为3的元素为
s[3]
print(s)
#列表的长度为
len(s)
print(s)
#排序
s.sort()
print(s)
#查询第一个3分的下标
a=s.index('3')
print (a)
#1分的同学有多少个
b=s.count('1')
print(b)
#查询3分的同学有多少个
c=s.count('3')
print(c)
3.简要描述列表与元组的异同。
不同点:①元组一旦被赋值,值不可以被改变;②列表可以任意的更改;③列表用方括号[],而元组用小括号()
相同点:元组和列表都是存储元素的容器,下标都是从0开始,以逗号分隔。