《Python编程:从入门到实践》学习笔记(知识点梳理+练习题答案代码)——第3章 列表简介

书籍说明

书名:《Python编程:从入门到实践》(第一版)/Python Crash Course: A Hands-On, Project-Based Introduction to Programming

作者:Eric Matthes(著)/袁国忠(译)

出版社:人民邮电出版社

开发环境说明

Python Version: 3.11.2

Python IDE: PyCharm Community Edition 2022.3.3


目录

第3章 列表简介

3.1 列表是什么

3.1.1 访问列表元素

3.1.2 索引从0而不是1开始

3.1.3 使用列表中的各个值

动手试一试

3.2 修改、添加和删除元素

3.2.1 修改列表元素

3.2.2 在列表中添加元素

3.2.3 从列表中删除元素

动手试一试

3.3 组织列表

3.3.1 使用函数sort( )对列表进行永久性排序

3.3.2 使用函数sorted( )对列表进行临时排序

3.3.3 倒着打印列表

3.3.4 确定列表的长度

动手试一试


第3章 列表简介

3.1 列表是什么

  • 列表,由一系列按特定顺序排列的元素组成。在Python中,用方括号([ ])来表示列表,并用逗号来分割其中的元素。下面是bicycles列表,里面包含四种自行车:
  • bicycles = ['trek', 'cannondale', 'redline', 'specialized']
    print(bicycles)

    输出结果:['trek', 'cannondale', 'redline', 'specialized']

3.1.1 访问列表元素

  • 列表是有序集合。要访问列表元素,可指出列表的名称,再指出元素的索引。下面从bicycles列表中提取第一款自行车:
  • bicycles = ['trek', 'cannondale', 'redline', 'specialized']
    print(bicycles[0].tittle)

    输出结果:Trek

3.1.2 索引从0而不是1开始

  • 索引从0开始,第一个列表元素的索引为0,第二个列表元素的索引为1。
  • 索引前面加一个负号(-),表示返回列表倒数第某个元素,-1指最后一个,-2指倒数第二个。
  • 同样以bicycles列表为例,依次访问列表的四个元素:
  • bicycles = ['trek', 'cannondale', 'redline', 'specialized']
    print(bicycles[0])
    print(bicycles[1])
    print(bicycles[2])
    print(bicycles[3])
    print(bicycles[-1])
    print(bicycles[-2])
    print(bicycles[-3])
    print(bicycles[-4])

    输出结果:

        trek
        cannondale
        redline
        specialized
        specialized
        redline
        cannondale
        trek

3.1.3 使用列表中的各个值

  • bicycles = ['trek', 'cannondale', 'redline', 'specialized']
    message = "My first bicycle was a " + bicycles[0].title() + "."
    print(message)

    输出结果:My first bicycle was a Trek.

动手试一试

3-1 姓名:将一些朋友的姓名存储在一个列表中,并将其命名为names。依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来。

  • names = ['shirley', 'emily', 'sophia', 'joy']
    print(names[0].title())
    print(names[1].title())
    print(names[2].title())
    print(names[3].title())

    输出结果:

        Shirley
        Emily
        Sophia
        Joy

3-2 问候语:继续使用练习3-1中的列表,但不打印每个朋友的姓名,而为每个人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

  • names = ['shirley', 'emily', 'sophia', 'joy']
    message = ": Happy Birthday!"
    print(names[0].title() + message)
    print(names[1].title() + message)
    print(names[2].title() + message)
    print(names[3].title() + message)

    输出结果:

        Shirley: Happy Birthday!
        Emily: Happy Birthday!
        Sophia: Happy Birthday!
        Joy: Happy Birthday!

3-3 自己的列表:想想你喜欢的通勤方式,如骑摩托车或开汽车,并创建一个包含多种通勤方式的列表。根据该列表打印一系列有关这些通勤方式的宣言,如“I would like to own a Honda motorcycle”。

  • commute = ['bicycle', 'motorcycle', 'car', 'taxi', 'subway', 'bus']
    message = "I would like to go to work by "
    print(message + commute[0] + ".")
    print(message + commute[1] + ".")
    print(message + commute[2] + ".")
    print(message + commute[3] + ".")
    print(message + commute[4] + ".")
    print(message + commute[5] + ".")

    输出结果:

        I would like to go to work by bicycle.
        I would like to go to work by motorcycle.
        I would like to go to work by car.
        I would like to go to work by taxi.
        I would like to go to work by subway.
        I would like to go to work by bus.

3.2 修改、添加和删除元素

3.2.1 修改列表元素

  • 下面有一个摩托车列表,我们想要把第一个元素honda修改成ducati:
  • motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles[0] = 'ducati'
    print(motorcycles)

    输出结果:

        ['honda', 'yamaha', 'suzuki']
        ['ducati', 'yamaha', 'suzuki']

3.2.2 在列表中添加元素

append( )函数:在列表末尾添加元素。

  • motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    motorcycles.append('ducati')
    print(motorcycles)

    输出结果:

        ['honda', 'yamaha', 'suzuki']
        ['honda', 'yamaha', 'suzuki', 'ducati']

  • motorcycle = []
    motorcycle.append('honda')
    motorcycle.append('yamaha')
    motorcycle.append('suzuki')
    motorcycle.append('ducati')
    print(motorcycle)

    输出结果:['honda', 'yamaha', 'suzuki', 'ducati']

insert( )函数:在列表中插入元素。

  • motorcycles = ['honda', 'yamaha', 'suzuki']
    motorcycles.insert(1,'ducati')
    print(motorcycles)

    输出结果:['honda', 'ducati', 'yamaha', 'suzuki']

3.2.3 从列表中删除元素

del语句:删除指定位置的元素。

  • 下面删除motorcycles列表中的第一个元素:
  • motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    del motorcycles[0]
    print(motorcycles)

    输出结果:

        ['honda', 'yamaha', 'suzuki']
        ['yamaha', 'suzuki']

pop( )函数:直接使用pop( )函数,可以删除列表的最后一个元素,并且提取这个元素。

  • motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles) #打印列表motorcycles
    popped_motorcycles = motorcycles.pop() #使用pop()函数,提取列表的最后一个元素,并存储到变量popped_motorcycles中
    print(motorcycles) #打印列表motorcycles,最后一个元素被删除
    print(popped_motorcycles) #打印变量popped_motorcycles,访问列表中被删除的值

    输出结果:

        ['honda', 'yamaha', 'suzuki']
        ['honda', 'yamaha']
        suzuki

  • motorcycles = ['honda', 'yamaha', 'suzuki']
    last_owned = motorcycles.pop()
    print("The last motorcycles I owned was a " + last_owned.title() + ".")

    输出结果:The last motorcycles I owned was a Suzuki.

pop( )函数:加上索引,可以删除列表指定位置的元素,并且提取这个元素。

  • motorcycles = ['honda', 'yamaha', 'suzuki']
    first_owned = motorcycles.pop(0)
    print("The first motorcycles I owned was a " + first_owned.title() + ".")

    输出结果:The first motorcycles I owned was a Honda.

注意:当你使用pop( )函数时,被弹出的元素就不再存在列表中了。

使用“del语句”还是“pop函数”,下面是一个简单的判断标准:如果你要从列表中删除一个元素,且不再以任何方式使用它——del语句;如果你要在删除元素后还能继续使用它——pop( )函数。

remove( )函数:根据值删除元素。

  • 前面介绍的“del语句”和“pop( )函数”,在使用时都需要知道被删除元素在列表中的具体位置。
  • 若未知被删除元素在列表所处的位置,只知道该元素的具体值,可以使用remove( )函数。
  • motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
    print(motorcycles)
    motorcycles.remove('ducati')
    print(motorcycles)

    输出结果:

        ['honda', 'yamaha', 'suzuki', 'ducati']
        ['honda', 'yamaha', 'suzuki']

注意:remove( )函数只删除第一个指定的值。如果要删除的值在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

动手试一试

3-4 嘉宾名单:如果你可以邀请任何人一起共进晚餐(无论是在世的还是故去的),你会邀请哪些人?请创建一个列表,其中包含至少3个你想邀请的人;然后,使用这个列表打印消息,邀请这些人来与你共进晚餐。

  • friends = ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
    print("My dear " + friends[0] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[1] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[2] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[3] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[4] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[5] + ", I would like to invite you to have dinner with me.")

    输出结果:

        My dear Rachel, I would like to invite you to have dinner with me.
        My dear Monica, I would like to invite you to have dinner with me.
        My dear Phoebe, I would like to invite you to have dinner with me.
        My dear Joey, I would like to invite you to have dinner with me.
        My dear Chandler, I would like to invite you to have dinner with me.
        My dear Ross, I would like to invite you to have dinner with me.

3-5 修改嘉宾名单:你刚得知有位嘉宾无法赴约,因此需要另外邀请一位嘉宾。

  • 以完成练习3-4时编写的程序为基础,在程序末尾添加一条print语句,指出哪位嘉宾无法赴约。
  • 修改嘉宾名单,将无法赴约的嘉宾的姓名替换为新邀请的嘉宾的姓名。
  • 再次打印一系列消息,向名单中的每位嘉宾发出邀请。
  • friends = ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
    popped_friends = friends.pop(2)
    friends.insert(2,'Lucy')
    print("My dear " + friends[0] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[1] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[2] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[3] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[4] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[5] + ", I would like to invite you to have dinner with me.")
    print("Unable to attend the appointment: " + popped_friends)

    输出结果:

        My dear Rachel, I would like to invite you to have dinner with me.
        My dear Monica, I would like to invite you to have dinner with me.
        My dear Lucy, I would like to invite you to have dinner with me.
        My dear Joey, I would like to invite you to have dinner with me.
        My dear Chandler, I would like to invite you to have dinner with me.
        My dear Ross, I would like to invite you to have dinner with me.
        Unable to attend the appointment: Phoebe

3-6 添加嘉宾:你刚找到了一个更大的餐桌,可容纳更多的嘉宾。请想想你还想邀请哪三位嘉宾。

  • 以完成练习3-4或练习3-5时编写的程序为基础,在程序末尾添加一条print语句,指出你找到了一个更大的餐桌。
  • 使用insert( )将一位新嘉宾添加到名单开头。
  • 使用insert( )将另一位新嘉宾添加到名单中间。
  • 使用append( )将最后一位新嘉宾添加到名单末尾。
  • 打印一系列消息,向名单中的每位嘉宾发出邀请。
  • friends = ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
    friends.insert(0,'Gunther')
    friends.insert(4,'Janice')
    friends.append('David')
    print("My dear " + friends[0] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[1] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[2] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[3] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[4] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[5] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[6] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[7] + ", I would like to invite you to have dinner with me.")
    print("My dear " + friends[8] + ", I would like to invite you to have dinner with me.")

    输出结果:

        My dear Gunther, I would like to invite you to have dinner with me.
        My dear Rachel, I would like to invite you to have dinner with me.
        My dear Monica, I would like to invite you to have dinner with me.
        My dear Phoebe, I would like to invite you to have dinner with me.
        My dear Janice, I would like to invite you to have dinner with me.
        My dear Joey, I would like to invite you to have dinner with me.
        My dear Chandler, I would like to invite you to have dinner with me.
        My dear Ross, I would like to invite you to have dinner with me.
        My dear David, I would like to invite you to have dinner with me.

3-7 缩减名单:你刚得知新购买的餐桌无法及时送达,因此只能邀请两位嘉宾。

  • 以完成练习3-6时编写的程序为基础,在程序末尾添加一行代码,打印一条你只能邀请两位嘉宾进行晚餐的消息。
  • 使用pop( )不断地删除名单中的嘉宾,直到只有两位嘉宾为止。每次从名单中弹出一位嘉宾时,都打印一条消息,让该嘉宾知悉你很抱歉,无法邀请他来共进晚餐。
  • 对于余下的两位嘉宾中的每一位,都打印一条消息,指出他依然在受邀人之列。
  • 使用del将最后两位嘉宾从名单中删除,让名单变成空的。打印该名单,核实程序结束时名单确实是空的。
  • friends = ['Gunther', 'Rachel', 'Monica', 'Phoebe', 'Janice', 'Joey', 'Chandler', 'Ross', 'David']
    
    print("I can only invite two friends to have dinner with me tonight.")
    
    popped_friends = friends.pop(0)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop(1)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop(1)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop(1)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop(1)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop(1)
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    popped_friends = friends.pop()
    print("My dear " + popped_friends +", I am so sorry to tell you that I can not invite you to have dinner with me tonight.")
    
    print("My dear " + friends[0] + ", I am glad to invite you to have dinner with me tonight.")
    print("My dear " + friends[1] + ", I am glad to invite you to have dinner with me tonight.")
    
    del friends[0]
    print(friends)
    del friends[0]
    print(friends)

    输出结果:

        I can only invite two friends to have dinner with me tonight.
        My dear Gunther, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Monica, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Phoebe, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Janice, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Joey, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Chandler, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear David, I am so sorry to tell you that I can not invite you to have dinner with me tonight.
        My dear Rachel, I am glad to invite you to have dinner with me tonight.
        My dear Ross, I am glad to invite you to have dinner with me tonight.
        ['Ross']
        []

3.3 组织列表

3.3.1 使用函数sort( )对列表进行永久性排序

  • sort( )函数:按照首字母顺序,对列表进行排序。此修改为永久性,无法恢复到原来的排序。
  • sort(reverse=Ture):按照与首字母顺序相反的顺序,对列表进行排序。同为永久性修改。
  • cars = ['bmw', 'audi', 'toyota', 'subaru']
    print(cars)
    cars.sort()
    print(cars)
    cars.sort(reverse=True)
    print(cars)

    输出结果:

        ['bmw', 'audi', 'toyota', 'subaru']
        ['audi', 'bmw', 'subaru', 'toyota']
        ['toyota', 'subaru', 'bmw', 'audi']

3.3.2 使用函数sorted( )对列表进行临时排序

  • sorted( )函数:保留列表元素原来的排列顺序,同时以特定的顺序呈现它们。
  • cars = ['bmw', 'audi', 'toyota', 'subaru']
    print("Here is the original list:")
    print(cars)
    print("\nHere is the sorted list:")
    print(sorted(cars))
    print("\nHere is the original list against:")
    print(cars)

    输出结果:

        Here is the original list:
        ['bmw', 'audi', 'toyota', 'subaru']

        Here is the sorted list:
        ['audi', 'bmw', 'subaru', 'toyota']

        Here is the original list against:
        ['bmw', 'audi', 'toyota', 'subaru']

3.3.3 倒着打印列表

  • reverse( )函数:反转列表元素的排列顺序。为永久性修改。
  • cars = ['bmw', 'audi', 'toyota', 'subaru']
    print(cars)
    cars.reverse()
    print(cars)

    输出结果:

        ['bmw', 'audi', 'toyota', 'subaru']
        ['subaru', 'toyota', 'audi', 'bmw']

3.3.4 确定列表的长度

  • len( )函数:可快速获悉列表的长度。
  • cars = ['bmw', 'audi', 'toyota', 'subaru']
    print(len(cars))

    输出结果:4

动手试一试

3-8 放眼世界:想出至少5个你渴望去旅游的地方。

  • 将这些地方存储在一个列表中,并确保其中的元素不是按字母顺序排列的。
  • 按原始排序顺序打印该列表。不要考虑输出是否整洁的问题,只管打印原始Python列表。
  • 使用sorted( )按字母顺序打印这个列表,同时不要修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用sorted( )按与字母顺序相反的顺序打印这个列表,同时不要修改它。
  • 再次打印该列表,核实排列顺序未变。
  • 使用reverse( )修改列表元素的排列顺序。打印该列表,核实排列顺序确实变了。
  • 使用reverse( )再次修改列表元素的排列顺序。打印该列表,核实已恢复到原来的排列顺序。
  • 使用sort( )修改该列表,使其元素按字母顺序排列。打印该列表,核实排列顺序确实变了。
  • 使用sort( )修改该列表,使其元素按与字母顺序相反的顺序排列。打印该列表,核实排列顺序确实变了。
  • cities = ['zhuhai', 'shanghai', 'guangzhou', 'shenzhen', 'beijing']
    print(cities)
    
    print(sorted(cities))
    print(cities)
    
    print(sorted(cities, reverse=True))
    print(cities)
    
    cities.reverse()
    print(cities)
    
    cities.reverse()
    print(cities)
    
    cities.sort()
    print(cities)
    
    cities.sort(reverse=True)
    print(cities)

    输出结果:

        ['zhuhai', 'shanghai', 'guangzhou', 'shenzhen', 'beijing']
        ['beijing', 'guangzhou', 'shanghai', 'shenzhen', 'zhuhai']
        ['zhuhai', 'shanghai', 'guangzhou', 'shenzhen', 'beijing']
        ['zhuhai', 'shenzhen', 'shanghai', 'guangzhou', 'beijing']
        ['zhuhai', 'shanghai', 'guangzhou', 'shenzhen', 'beijing']
        ['beijing', 'shenzhen', 'guangzhou', 'shanghai', 'zhuhai']
        ['zhuhai', 'shanghai', 'guangzhou', 'shenzhen', 'beijing']
        ['beijing', 'guangzhou', 'shanghai', 'shenzhen', 'zhuhai']
        ['zhuhai', 'shenzhen', 'shanghai', 'guangzhou', 'beijing']

3-9 晚餐嘉宾:在完成练习3-4~练习3-7时编写的程序之一中,使用len( )打印一条消息,指出你邀请了多少位嘉宾来与你共进晚餐。

  • friends = ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
    print("I had invited " + str(len(friends)) + " friends to have dinner with me.")

    输出结果:I had invited 6 friends to have dinner with me.

3-10 尝试使用各个函数:想想可存储到列表中的对象,如山岳、河流、国家、城市、语言或你喜欢的任何东西。编写一个程序,在其中创建一个包含这些元素的列表,然后,对于本章介绍的每个函数,都至少使用一次来处理这个列表。

  • friends = ['Rachel', 'Monica', 'Phoebe']
    friends.append('Ross')
    print(friends)
    friends.insert(3,'Joey')
    print(friends)
    friends.insert(4,'Chandler')
    print(friends)
    friends.append('Lucy')
    friends.append('Lisa')
    friends.append('Jennie')
    print(friends)
    del friends[6]
    print(friends)
    popped_friends = friends.pop()
    print(popped_friends)
    print(friends)
    friends.remove('Lisa')
    print(friends)
    print(sorted(friends))
    print(sorted(friends,reverse=True))
    friends.sort()
    print(friends)
    friends.sort(reverse=True)
    print(friends)
    friends.reverse()
    print(friends)
    print("I have " + str(len(friends)) + " friends.")

    输出结果:

        ['Rachel', 'Monica', 'Phoebe', 'Ross']
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Ross']
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross', 'Lucy', 'Lisa', 'Jennie']
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross', 'Lisa', 'Jennie']
        Jennie
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross', 'Lisa']
        ['Rachel', 'Monica', 'Phoebe', 'Joey', 'Chandler', 'Ross']
        ['Chandler', 'Joey', 'Monica', 'Phoebe', 'Rachel', 'Ross']
        ['Ross', 'Rachel', 'Phoebe', 'Monica', 'Joey', 'Chandler']
        ['Chandler', 'Joey', 'Monica', 'Phoebe', 'Rachel', 'Ross']
        ['Ross', 'Rachel', 'Phoebe', 'Monica', 'Joey', 'Chandler']
        ['Chandler', 'Joey', 'Monica', 'Phoebe', 'Rachel', 'Ross']
        I have 6 friends.

你可能感兴趣的:(学习,python)