- 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
w ='''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'vebeen lookin' 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 outof 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 Selena : I just hope you'r lyin' next to somebody Know it's hard to love yalike me Must be a good reason that you're gone Every now and then I think you might want me to come show up yourdoor But I'm just too afraidthat i'll be worng Don't wanna know If you'ra lookin' intoher eyes If she's holdin' onto youso tight The way i did before I overdosed Should've know your lovewas a game Now I can't get'cha outof my brain Ooh it's such a shame ''' w = w.lower() w = w.replace('?',' ') words=w.split(' ') print(words) print("talk出现的次数为:"+str(w.count('talk')))
- 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
grade=list('2333123456') print('评分列表:',grade) for i in range(len(grade)): grade[i]=int(grade[i]) print('作业成绩',grade) grade.append(7) print('增加一个学生成绩:',grade) grade.pop(1) print('删除下标为1的同学的成绩:',grade) grade[4]=3 print('把下标为4的同学的成绩修改成3:',grade) grade.index(3) print('第一个3分的下标:',grade) print("得1分的同学有:"+str(grade.count(1))+"个") print("得3分的同学有:"+str(grade.count(3))+"个")
运行结果:
= RESTART: C:/Users/dell/AppData/Local/Programs/Python/Python36/python作业7.py =
评分列表: ['2', '3', '3', '3', '1', '2', '3', '4', '5', '6']
作业成绩 [2, 3, 3, 3, 1, 2, 3, 4, 5, 6]
增加一个学生成绩: [2, 3, 3, 3, 1, 2, 3, 4, 5, 6, 7]
删除下标为1的同学的成绩: [2, 3, 3, 1, 2, 3, 4, 5, 6, 7]
把下标为4的同学的成绩修改成3: [2, 3, 3, 1, 3, 3, 4, 5, 6, 7]
第一个3分的下标: [2, 3, 3, 1, 3, 3, 4, 5, 6, 7]
得1分的同学有:1个
得3分的同学有:4个
>>>
3.简要描述列表与元组的异同。
答:
同:列表和元组十分类似,列表和项目都可以添加删除或是搜索列表中的项目。
异:列表中的项目应该包括在方括号中,元组的项目应该包括在圆括号中;列表是可变的数据类型,即这种类型是可以被改变的,但元组是不可变的,也就是说你不能修改元组。