1. 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。

 

news='''All the times that you rain on my parade
And all the clubs you get in using my name
You think you broke my heart
Ohhh girl for goodness sake
You think I'm crying
Oh my ohhh, well I ain't!
And I didn't wanna write a song
'Cause I didn't want anyone thinking I still care, I don't
But, you still hit my phone up
And baby I be moving on
And I think you should be somethin' I don't wanna hold back
Maybe you should know that
My mama don't like you and she like's everyone'''
news=news.lower()
for i in ',!':
    news=news.replace(i,' ')
words=news.split(' ')
print(words)
print('you:',words.count('you'))
print('and:',words.count('and'))

 

2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。

 

pf=list('12321222132121213320')
for i in range(len(pf)):
    pf[i]=int(pf[i])
print(pf)
print(pf.index(3))
print(pf.count(1))
print(pf.count(3))

 

3.简要描述列表与元组的异同。

列表和元组非常类似,有时候他们都干一样的事情。他们最大的区别是元组一旦被赋值,值不可以被改变,一旦改变就会出错;但是列表可以任意的更改。其次的区别是他们用不同的符号表示,复制的时候,列表用方括号[],而元组用小括号()。

posted on 2017-09-20 21:23  034陈妍希  阅读(92)  评论(0编辑  收藏  举报