1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
s=('''I'm a big, big girl
in a big, big world
It's not a big,big thing if you leave me
But I do, do feel
that I do, do will
miss you much
Miss you much
I can see the first leaf falling
It's all yellow and nice
It's so very cold outside
Like the way I'm feeling inside
I'm a big, big girl
in a big big world
It's not a big, big thing if you leave me
But I do, do feel
that I do, do will
miss you much
Miss you much
Outside it's now raining
And tears are falling from my eyes
Why did it have to happen
Why did it all have to end
I'm a big, big girl
in a big, big world
It's not a big, big thing if you leave me
But I do, do feel
that I do, do will
miss you much
Miss you much
I have your arms around me
Warm like fire
But when I open my eyes
You're gone
I'm a big, big girl
in a big, big world
It's not a big, big thing if you leave me
But I do, do feel
that I do, do will
miss you much
Miss you much
I'm a big, big girl
in a big, big world
It's not a big, big thing if you leave me
But I do feel I will
miss you much
Miss you much''')
s=s.lower()
for i in ',':
s=s.replace(i,' ')
words=s.split(' ')
print(s)
print(s.lower())
print(s.count('big'))
print(s.split(' '))
结果:
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i can see the first leaf falling
it's all yellow and nice
it's so very cold outside
like the way i'm feeling inside
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
outside it's now raining
and tears are falling from my eyes
why did it have to happen
why did it all have to end
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i have your arms around me
warm like fire
but when i open my eyes
you're gone
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do feel i will
miss you much
miss you much
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i can see the first leaf falling
it's all yellow and nice
it's so very cold outside
like the way i'm feeling inside
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
outside it's now raining
and tears are falling from my eyes
why did it have to happen
why did it all have to end
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i have your arms around me
warm like fire
but when i open my eyes
you're gone
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do do feel
that i do do will
miss you much
miss you much
i'm a big big girl
in a big big world
it's not a big big thing if you leave me
but i do feel i will
miss you much
miss you much
30
["i'm", 'a', 'big', '', 'big', 'girl
in', 'a', 'big', '', 'big', "world
it's", 'not', 'a', 'big', 'big', 'thing', 'if', 'you', 'leave', 'me
but', 'i', 'do', '', 'do', 'feel
that', 'i', 'do', '', 'do', 'will
miss', 'you', 'much
miss', 'you', 'much
i', 'can', 'see', 'the', 'first', 'leaf', "falling
it's", 'all', 'yellow', 'and', "nice
it's", 'so', 'very', 'cold', 'outside
like', 'the', 'way', "i'm", 'feeling', "inside
i'm", 'a', 'big', '', 'big', 'girl
in', 'a', 'big', 'big', "world
it's", 'not', 'a', 'big', '', 'big', 'thing', 'if', 'you', 'leave', 'me
but', 'i', 'do', '', 'do', 'feel
that', 'i', 'do', '', 'do', 'will
miss', 'you', 'much
miss', 'you', 'much
outside', "it's", 'now', 'raining
and', 'tears', 'are', 'falling', 'from', 'my', 'eyes
why', 'did', 'it', 'have', 'to', 'happen
why', 'did', 'it', 'all', 'have', 'to', "end
i'm", 'a', 'big', '', 'big', 'girl
in', 'a', 'big', '', 'big', "world
it's", 'not', 'a', 'big', '', 'big', 'thing', 'if', 'you', 'leave', 'me
but', 'i', 'do', '', 'do', 'feel
that', 'i', 'do', '', 'do', 'will
miss', 'you', 'much
miss', 'you', 'much
i', 'have', 'your', 'arms', 'around', 'me
warm', 'like', 'fire
but', 'when', 'i', 'open', 'my', "eyes
you're", "gone
i'm", 'a', 'big', '', 'big', 'girl
in', 'a', 'big', '', 'big', "world
it's", 'not', 'a', 'big', '', 'big', 'thing', 'if', 'you', 'leave', 'me
but', 'i', 'do', '', 'do', 'feel
that', 'i', 'do', '', 'do', 'will
miss', 'you', 'much
miss', 'you', "much
i'm", 'a', 'big', '', 'big', 'girl
in', 'a', 'big', '', 'big', "world
it's", 'not', 'a', 'big', '', 'big', 'thing', 'if', 'you', 'leave', 'me
but', 'i', 'do', 'feel', 'i', 'will
miss', 'you', 'much
miss', 'you', 'much']
>>>
2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
>>> grades=list('123321123')
>>> print(grades)
['1', '2', '3', '3', '2', '1', '1', '2', '3']
>>> grades.pop(2)
'3'
>>> print(grades)
['1', '2', '3', '2', '1', '1', '2', '3']
>>> grades[1]=grades[4]
>>> print(grades)
['1', '1', '3', '2', '1', '1', '2', '3']
>>> grades.index(3)
3.简要描述列表与元组的异同。
list是一种有序的集合,可以随时添加和删除其中的元素。
tuple和list非常类似,但是tuple一旦初始化就不能修改。