• 第三章:列表简介


     

      1)列表由一系按特定顺序排列的元素组成,在Python中,用方括号 []来表示列表,并用逗号来分隔其中的元素
       
    例:bicycles=['trek','cannondale','redline'....]
                 print(bicycles)
     
     
    3.1.1 访问列表元素
      1)列表是有序集合,因此要访问列表的任何元素,只需将改元素的位置或索引告诉Python即可
         
    例:bicycles=['trek','cannondale','redline'....]
                 print(bicycles[0])  代码优化  print(bicycles[0].title())
     
     
    3.1.2 索引从0而不是1开始    bicycles.py   bicycles_messae.py
      1)在Python中,第一个列表元素的索引为0,而不是1.
      2)第二个列表元素的索引为1
     
      3)Python为访问最后一个列表元素提供了一种特殊方法。通过将索引指定为 -1,可让Python返回最后一个列表元素,索引指定为-2,可让Python返回倒数第二个元素...以此类推
        
    例:bicycles=['trek','cannondale','redline',‘specialized’]
                  print(bicycles[-1])        print(bicycles[-2])
                     specialized                  redline
     
     
     
     
    3.2.1 修改列表元素    motocycles_修改列表元素.py
    修改列表元素的语法与访问列表元素的语法类似。要修改列表元素,可指定列表名和要修改的索引,再指定该元素的新值
        
    例:motorcycles=['honda','yamaha','suzuki']
                 motorcycles[1]='duct'
                    print(motorcycles)
     
     
     
    3.2.2 添加列表元素   motocycles_添加列表元素.py   motocycles_插入列表元素.py
    1)在列表中添加新元素时,最简单的方式是将元素附加到列表末尾。
     
    2)变量名.append(‘列值’)方法:在列表末尾添加元素,给列表附加元素时,它将添加到列表末尾。继续使用前一个示例中的列表,在其末尾添加新元素'ducati'
       
    例:motorcycles=['honda','yamaha','suzuke']
                  motorcycles.append('ducati')
                    print(motorcycles)
     
     
     
    3)变量名.insert(索引号,‘列值’)方法:在列表中插入元素,该方法可在列表任意位置添加新元素,所以需指定新元素的索引和值
        
    例:motorcycles=['honda','yamaha','suzuke']
                  motorcycles.insert(0,'ducati')
                    print(motorcycles)
     
     
     
    3.2.3 删除列表元素
    1)使用del语句删除元素
        
    例:motorcycles=['honda','yamaha','suzuke']
                  del motorcycles[0]  或任何位置处的列表元素
                    print(motorclcles)
     
     
    2)使用pop()方法可删除列表末尾的元素,并让你能够接着使用它
     假设列表中的摩托车是按购买时间存储的,就可使用pop()方法,指出最后购买的是哪款摩托车
        
    例:motorcycles=['honda','yamaha','suzuke']
              last_owned=motorcycles.pop()
            print("The last motorcycle I owned was a "+last_owned.title())
     
     
    3)使用pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素索引即可
       
    例:motorcycles=['honda','yamaha','suzuke']
              first_owned=motorcycles.pop(0) 或任何位置处的列表元素
                    print('The first motorcycle I owned was a '+first_owned.title())
     
    4)当你不知道列表中删除的值所在的位置,如果你只知道要删除的元素值可使用 变量名.remove()方法
        
    例:motorcycles=['honda','yamaha','suzuke','ducati']
                  motorcycles.remove('ducati')
                    print(motorcycles)
     
     
     
    3.3 组织列表
    3.3.1 使用方法sort()对列表进行永久性排序
    1)变量名.sort()方法让列表按首字母顺序排序
        
    例:cars=['bmw','audi','toyota','subaru']
                  cars.sort()
                    print(cars) ===>audi,bmw,subaru,toyota
     
     
    2)还可以按与字母顺序相反的方向顺序排列元素,只需向sort()方法传递参数reverse=Ture
        
    例:cars=['bmw','audi','toyota','subaru']
                  cars.sort(reverse=Ture)
                    print(cars)    ===>toyota,subaru,bmw,audi
     
     
    3.3.2 使用sorted()对列表进行临时排序
    1)sorted(变量名)方法让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序
       
    例:cars=['bmw','audi','toyota','subaru']
                  print('Here is the sorted list:')
                    print(sorted(cars))
     
     
    2)sorted(变量名,reverse=True)方法可对列表进行临时性首字母倒序排列
        
    例:cars=['bmw','audi','toyota','subaru']
                  print('Here is the sorted(reverse) list:')
                    print(sorted(cars,reverse=True))
     
     
     
    3.3.3 倒着打印列表
    1)使用reverse()是反转列表元素的排列顺序,而不是与字母顺序相反的顺序排列元素
    2)reverse()永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此只
    需对列表再次调用reverse()即可
       
    例:cars['bmw','audi','toyota','subaru']
                  cars.reverse()
                    print(cars)
     
     
     
    补充:1)reverse()方法和sort()方法的区别:
           reverse()方法:是以整个列表元素的反向顺序进行排列
           
    例:cars['bmw','audi','toyota','subaru']
                     cars.reverse()
                       print(cars)  ===> subaru,toyota,audi,bmw
     
     
             sort()方法:是以列表字母顺序进行排列
           
     例:cars['bmw','audi','toyota','subaru']
                     cars.sort()
                       print(cars)  ===> audi,bmw,subaru,toyota
     
     
         2)sorted()方法和sort()方法的区别:
            sorted()方法:临时性改变列表元素
            sort()方法:永久性改变列表元素
     
            3)sorted()方法和sort()方法的共同点:
                    都是以大写字母优先排列,再次以首字母顺序排列
     
     
     
    3.3.4 确定列表的长度
    1)使用函数len()可快速获悉列表的长度
      
      例:===>cars=['bmw','audi','toyota','subaru']
                  ===>len(cars)
                        4
     
     
    3.4 使用列表时避免索引错误
        
    例1:motorcycles=['honda','yamaha','suzuki']
                  print(motorcycles[3])
                      IndexError:list index out of range
     
     
            
    例2:motorcycles=[]
                  print(motorcycles[-1])
                      IndexError:list index out of range
     
                  列表motorcycles不包含任何元素,因此Python返回一条索引错误信息
    student_view
     
     
     
  • 相关阅读:
    RH133读书 笔记(3)
    RH033读书笔记(16)-Lab 17 Installation and Administration Tools
    RH033读书笔记(15)-Lab 16 The Linux Filesystem
    RH033读书笔记(14)-Lab 15 Switching Users and Setting a Umask
    RH033读书笔记(13)-Lab 14 Network Clients
    RH033读书笔记(12)-Lab 13 Finding and Processing Files
    RH033读书笔记(11)-Lab 12 Configuring the bash Shell
    RH033读书笔记(10)-Lab 11 Process Control
    RH033读书笔记(9)-Lab 10 Understanding the Configuration Tools
    RH033读书笔记(8)-Lab 9 Using vim
  • 原文地址:https://www.cnblogs.com/gaodi2345/p/11412876.html
Copyright © 2020-2023  润新知