1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
str='''Never look back,we saidHow was I to knowI'd miss you so?Loneliness up ahead,emptiness behindWhere do I go?And you didn't hearAll my joy through my tearsAll my hopes through my fearsDid you knowStill I miss you somehowFrom the bottom of my broken heartThere's just a thingor two I'd like you to knowYou were my first love,you were my true loveFrom the first kisses tothe very last roseFrom the bottom of my broken heartEven though time mayfind me somebody newYou were my real love,I never knew love'Til there was youFrom the bottom of my broken heartBaby,I said,please stay.Give our love a chancefor one more dayWe could have worked things outTaking time iswhat love is all aboutBut you put a dartThrough my dreamsthrough my heartAnd I'm back where Istarted again'''str=str.lower()str=str.replace('?',' ')str=str.replace("'",' ')str=str.replace(',',' ')str=str.replace('.',' ')print(str)str=str.count('love')print("love出现的次数:",str)
2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
wl=list('2213133122')
print(list)print(wl)wl.insert(5,'2')print("增:",wl)wl.pop(3)print("删:",wl)wl[1]='3'print("改:",wl)i=wl.index('3')print("第一个3的下标:",i)s=wl.count('1')print("1分有:",s,"个")t=wl.count('2')print("2分有:",t,"个")r=wl.count('3')print("3分有:",r,"个")
3.简要描述列表与元组的异同。
答:同:列表与元组都是非常类似的,是内置的有序集合,都是可以嵌套的。
异:列表是一种有序的序列,正向递增、反向递减序号,可以随时添加和删除其中的元素,是可变的;而元组一旦初始化就不能修改了,是不可变的。