实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
SONG='''Look, if you had one shot, one opportunity
To seize everything you ever wanted…One moment
Would you capture it or just let it slip?
His palms are sweaty, knees weak, arms are heavy
There's vomit on his sweater already, mom's spaghetti
He's nervous, but on the surface he looks calm and ready
To drop bombs, but he keeps on forgettin
What he wrote down, the whole crowd goes so loud
He opens his mouth, but the words won't come out
He's chokin, how everybody's jokin now
The clock's run out, time's up over, bloah!
Snap back to reality, Oh there goes gravity
Oh, there goes Rabbit, he choked
He's so mad, but he won't give up that
Is he? No
He won't have it , he knows his whole back city's ropes'''
song=str.lower(SONG)
song=song.replace(', ',' ')
song=song.replace('!',' ')
song=song.replace('?',' ')
words=song.split(' ')
print(song)
print(words)
print(song.count('go'))

列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
s=list('1231321321213')
#增
s.append('3')
print(s)
#删
s.pop(0)
print(s)
#改
s.insert(7,'3')
print(s)
#查询
s.index('3')
print(s)
print(s.count('3'))
print(s.count('1'))

简要描述列表与元组的异同。
列表与元组都是容器,是一系列的对象;二者都可以包含任意类型的元素甚至可以是一个序列,还可以包含元素的顺序
列表可以增加删除修改,元祖不可以。
本文通过实例演示了如何使用Python进行文本预处理,包括下载歌词或文章,转换大小写,去除标点符号,统计单词频率,以及创建和操作评分列表。详细介绍了列表和元组的区别,展示了Python在数据处理方面的强大功能。
571

被折叠的 条评论
为什么被折叠?



