1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
str='''We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do I just heard you found the one you've been lookin' The one you been looking for I wish i would've konwn that wasn't me Cause even after all this time i still wonder Why i can't move on? Just the way you dance so easliy Don't wanna know The kinda dress you're wearin' tonight If he's holdin' onto you so tight The way i did before I overdosed Should've known your love was game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do I just hope you'r lyin' next to somebody Know it's hard to love ya like me Must be a good reason that you're gone Every now and then I think you might want me to come show up your door But I'm just too afraid that i'll be worng Don't wanna know If you'ra lookin' into her eyes If she's holdin' onto you so tight The way i did before I overdosed Should've know your love was a game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do Like we used to do Don't wanna know The kinda dress you're wearin' tonight If he's givin' it to you just right The way i did before I overdosed Should've know your love was a game Now I can't get'cha out of my brain Ooh it's such a shame We don't talk anymore We don't talk anymore We don't talk anymore Like we used to do We don't laugh anymore What was all of it for? We don't talk anymore Like we used to do We don't talk anymore The way did before We don't talk anymore Ooh Woo Ooh it's such a shame We don't talk anymore ''' for i in ",.?! ": str=str.replace(i," ") str=str.lower() str=str.split(" ") print(str) print(str.count("we"))
2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
lis=list("13221231313123") print(lis) lis.append("2") print("在最后插入一个2:") print(lis) lis.insert(2,"1") print("在下标为2的地方插入一个1:") print(lis) lis.pop() print("删除最后一个元素:") print(lis) ind=lis.index("3") print("第一个三分的下标为:",ind) print("一分的同学有:",lis.count("1"),"个") print("三分的同学有:",lis.count("3"),"个")
3、简要描述列表与元组的异同。
答: 列表和元组大致相同,都是有序的存储元素,并且每个元素都有下标可直接查询。不同在于输出时列表是用一对 [ ] 括号括起来,元组为 ( ) 括号,然后是列表里面的元素是可以使用插删改等方法改变的,也可以使用排序等方式改变元素存储位置,而元组不行,元组只能进行对内容进行查询,不能改变其内容。