实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
sorry='''You gotta go and get angry at all of my honesty You know I try but I don’t do too well with apologies I hope I don’t run out of time, could someone call a referee? Cause I just need one more shot at forgiveness I know you know that I made those mistakes maybe once or twice By once or twice I mean maybe a couple a hundred times So let me, oh let me redeem, oh redeem, oh myself tonight Cause I just need one more shot at second chances Yeah, is it too late now to say sorry? Cause I’m missing more than just your body Is it too late now to say sorry? Yeah I know that I let you down Is it too late to say I’m sorry now? I’m sorry, yeah Sorry, yeah Sorry Yeah I know that I let you down Is it too late to say sorry now? I’ll take every single piece of the blame if you want me to But you know that there is no innocent one in this game for two I’ll go, I’ll go and then you go, you go out and spill the truth Can we both say the words and forget this? Is it too late now to say sorry? Cause I’m missing more than just your body Is it too late now to say sorry? Yeah I know that I let you down Is it too late to say I’m sorry now? I’m not just trying to get you back on me Cause I’m missing more than just your body Is it too late now to say sorry? Yeah I know that I let you down Is it too late to say sorry now? I’m sorry, yeah Sorry, oh Sorry Yeah I know that I let you down Is it too late to say sorry now? I’m sorry, yeah Sorry, oh Sorry Yeah I know that I let you down Is it too late to say sorry now?''' sorry=sorry.lower()#首字母小写 for i in ',?': sorry=sorry.replace(i,' ')#替换所有的,? word=sorry.split(' ')#以‘ ’断开分成单独的字符串 print(word) print('sorry:',word.count('sorry')) print('now:',word.count('now')) print('say:',word.count('say'))
列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
pf=list('12321222132121213320')#由字符串创建一个作业评分列表 for i in range(len(pf)): pf[i]=int(pf[i])#字符串转化为整形 print(pf)#输出整型评分表 print(pf.index(3))#查询第一个3分的下标 print(pf.count(1))#统计1分的同学个数 print(pf.count(3))#统计3分的同学个数
简要描述列表与元组的异同。
列表和元组非常类似,有时候他们都干一样的事情。他们最大的区别是元组一旦被赋值,值不可以被改变,一旦改变就会出错;但是列表可以任意的更改。其次的区别是他们用不同的符号表示,复制的时候,列表用方括号[],而元组用小括号()。