1、实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
>>> bin='''Put your make up on
Get your nails done
Curl your hair
Run the extra mile
Keep it slim
so they like you, do they like you?
Get your sexy on
Don't be shy, girl
Take it off
This is what you want, to belong, so they like you
Do you like you?
You don't have to try so hard
You don't have to, give it all away
You just have to get up, get up, get up, get up
You don't have to change a single thing
You don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to try
Yooou don't have to try
Oooh Oooh
Get your shopping on, at the mall, max your credit card
You don't have to choose, buy it all, so they like you
Do they like you?
Wait a second,Why, should you care, what they think of you
When you're all alone, by yourself, do you like you?
Do you like you?You don't have to try so hard
You don't have to, give it all away
You just have to get up, get up, get up, get up
You don't have to change a single thing
You don't have to try so hard
You don't have to bend until you break
You just have to get up, get up, get up, get up
You don't have to change a single thing
You don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to try
You don't have to try
You don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to tryYooou don't have to try
NooooOooh
You don't have to try so hard
You don't have to, give it all away
You just have to get up, get up, get up, get up
You don't have to change a single thing
You don't have to try, try, try, tryYou don't have to try, try, try, try
You don't have to try, try, try, try
You don't have to try
Take your make up off
Put your head down
Take a breath
Look into the mirror, at yourself.
Don't you like you?
Cause I like you!''
>>> bin=bin.replace(',',' ')
>>> bin=bin.replace('?',' ')
>>> bin=bin.replace('!',' ')
>>> bin=bin.replace('.',' ')
>>> bin.islower()
False
>>> bin=bin.lower()
>>> bin.islower()
True
>>> print(bin.count('try'))
55
>>> print(bin.count('you'))
29
>>> words=bin.split(' ')
>>> print(words)
2、列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
>>> grade=list('1231211233321221221')
>>> grade
['1', '2', '3', '1', '2', '1', '1', '2', '3', '3', '3', '2', '1', '2', '2', '1', '2', '2', '1']
for i in range(len(grade)):
grade[i]=int(grade[i])
print(grade)
>>>grade.append(2)
3、简要描述列表与元组的异同。
不同点:元组不可修改,列表可以修改
相同点:除了修改这个点,元组和列表差不多,都可以进行计数,查询等。