1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
bad='''your butt is mine
I Gonna tell you right
Just show your face
In broad daylight
I'm telling you
On how I feel
Gonna Hurt Your Mind
Don't shoot to kill
Shamone
Shamone
Lay it on me
All right
I'm giving you
On count of three
To show your stuff
Or let it be
I'm telling you
Just watch your mouth
I know your game
What you're about
Well they say the sky's the limit
And to me that's really true
But my friend you have seen nothin'
Just wait till I get through
Because I'm bad,I'm bad
shamone
(Bad,bad,really,really bad)
You know I'm bad,I'm bad
(Bad,bad,really,really bad)
You know it
You know I'm bad,I'm bad
Come on,you know
(Bad,bad,really,really bad)
And the whole world
Has to answer right now
Just to tell you once again
Who's bad
The word is out
You're doin' wrong
Gonna lock you up
Before too long
Your lyin' eyes
Gonna tell you right
So listen up
Don't make a fight
Your talk is cheap
You're not a man
Your throwin' stones
To hide your hands
Well they say the sky's the limit
And to me that's really true
But my friend you have seen nothin'
Just wait till I get through
Because I'm bad,I'm bad
shamone
(Bad,bad,really,really bad)
You know I'm bad,I'm bad
(Bad,bad,really,really bad)
You know it
You know I'm bad,I'm bad
Come on,you know
(Bad,bad,really,really bad)
And the whole world
Has to answer right now
Just to tell you once again
Who's bad
We could change the world tomorrow
This could be a better place
If you don't like what I'm sayin'
Then won't you slap my face
Because I'm bad'''
bad=bad.lower()
for i in ",.?!":
bad=bad.replace(i,' ')
bad=bad.replace('\n',' ')
words=bad.split(' ')
print(words)
print("bad出现的次数为:"+str(words.count("bad")))
print("you出现的次数为:"+str(words.count("you")))
运行结果为:
2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
score=list('13213212132132132')
print(score)
for i in range(len(score)):
score[i-1]=int(score[i-1])
for i in range(len(score)):
if(score[i-1]==3):
print("第一个3分的下标为:"+str(i-1))
break
print("得2分的同学有"+str(score.count(2))+"个")
print("删除列表下标为6的值")
score.pop(6)
print(score)
print("追加一个值“4”")
score.append(4)
print(score)
print("在列表下标第1位插入一个7")
score.insert(1,7)
print(score)
运行结果为:
3.简要描述列表与元组的异同。
答:列表与元组很想象。不同的是列表里的数据可以更改,而元组无法进行修改。