1.实例: 下载一首英文的歌词或文章,将所有,.?!等替换为空格,将所有大写转换为小写,统计某几个单词出现的次数,分隔出一个一个的单词。
lyrics=‘‘‘Waking up I see that everything is okay The first time in my life and now it‘s so great Slowing down I look around and I am so amazed I think about the little things that make life great I wouldn‘t change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don‘t go away I need you now And I‘ll hold on to it, don‘t you let it pass you by I found a place so safe, not a single tear The first time in my life and now it‘s so clear Feel calm, I belong, I‘m so happy here It‘s so strong and now I let myself be sincere I wouldn‘t change a thing about it This is the best feeling This innocence is brilliant, I hope that it will stay This moment is perfect, please don‘t go away I need you now And I‘ll hold on to it, don‘t you let it pass you by It‘s the state of bliss you think you‘re dreaming It‘s the happiness inside that you‘re feeling It‘s so beautiful, it makes you wanna cry It‘s the state of bliss you think you‘re dreaming It‘s the happiness inside that you‘re feeling It‘s so beautiful, it makes you wanna cry It‘s so beautiful, it makes you wanna cry This innocence is brilliant, it makes you wanna cry This innocence is brilliant, please don‘t go away ‘Cause I need you now And I‘ll hold on to it, don‘t you let it pass you by This innocence is brilliant, it‘s so beautiful, it‘s so beautiful This moment is perfect, please don‘t go away I need you now, it makes me wanna cry And I‘ll hold on to it, don‘t you let it pass you by‘‘‘ lyrics=lyrics.lower() lyrics=lyrics.replace(‘,‘,‘ ‘) print(lyrics) words=lyrics.split(‘ ‘) print(words)
运行结果为:
2.列表实例:由字符串创建一个作业评分列表,做增删改查询统计遍历操作。例如,查询第一个3分的下标,统计1分的同学有多少个,3分的同学有多少个等。
ls=list(‘12134165135241335363‘) for i in range (len(ls)): ls[i]=int(ls[i]) print(ls) print(ls.index(3)) print(ls.count(1)) print(ls.count(3))
运行结果为:
3.简要描述列表与元组的异同。
答:元组和列表基本一样,列表与元组都是容器,是一系列的对象,二者都可以包含任意类型的元素甚至可以是一个序列,还可以包含元素的顺序
但是列表可以修改列表元素,而元组不可以修改元素
时间: 2024-10-26 11:14:23