- 实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
a='''Panda is favored by people, because they are so lovely. Many people come to Sichuan to see the pandas in the zoo, but the media always report some bad behaviors. Some tourists throw away the rubbish and some adults let their kids stand in the handrail, just to get closer to pandas. We need to act politely, for protecting the environment and animals. ''' for i in ',.?': a=a.replace(i,' ') a=a.lower() write=a.split(' ') print(a) print(a.count('panda'))
- 列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
a=list('123122312331123') >>> a ['1', '2', '3', '1', '2', '2', '3', '1', '2', '3', '3', '1', '1', '2', '3'] >>> for i in range(len(a)): a[i]=int(a[i]) >>> a [1, 2, 3, 1, 2, 2, 3, 1, 2, 3, 3, 1, 1, 2, 3] >>> a.append(3) >>> a [1, 2, 3, 1, 2, 2, 3, 1, 2, 3, 3, 1, 1, 2, 3, 3] >>> a.insert(2,2) >>> a [1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 3, 3, 1, 1, 2, 3, 3] >>> a.pop(2) 2 >>> a [1, 2, 3, 1, 2, 2, 3, 1, 2, 3, 3, 1, 1, 2, 3, 3] >>> a.index(3) 2 >>> a.count(1) 5 >>> a.count(3) 6 >>>
- 简要描述列表与元组的异同。
列表可以增加或删除项目,是可变的数据类型,即这种类型是可以被改变的。元组是不可变的,即你不能修改元组。