python学习之路--python基础

发布时间:2019-03-13 22:28:32编辑:auto阅读(1981)

    本文主要介绍下python列表的的一些基本操作

    列表是我们在python编程中常用的数据类型之一,通过列表我们可以对数据实现最方便的存储、修改等操作。

    定义列表

     1 names=['ZhangSan','LiSi','WangWu'] 

    列表的访问:通过下表来访问列表中的元素,下标从0开始计数,还可以反着取,-1表示最后一个元素

    1 >>> names[0]
    2 'ZhangSan'
    3 >>> names[2]
    4 'WangWu'
    5 >>> names[-1]
    6 'WangWu'
    7 >>> names[-2]
    8 'LiSi'
    9 >>>

    切片操作

     1 >>> names=['ZhangSan','LiSi','WangWu','ZhaoLiu','Tom','Amy']
     2 >>> names[1:4]  #取下标1到4之间的数据元素,包括1,不包括4
     3 ['LiSi', 'WangWu', 'ZhaoLiu']
     4 >>> names[1:-1]  #取小标1到-1的值,不包括-1
     5 ['LiSi', 'WangWu', 'ZhaoLiu', 'Tom']
     6 >>> names[0:3]
     7 ['ZhangSan', 'LiSi', 'WangWu']
     8 >>> names[:3]  #如果是从头开始取,0可以忽略,跟上句结果一样
     9 ['ZhangSan', 'LiSi', 'WangWu']
    10 >>> names[3:]  #如果想取最后一位,必须不能写-1
    11 ['ZhaoLiu', 'Tom', 'Amy']
    12 >>> names[3:-1]  #不包含-1
    13 ['ZhaoLiu', 'Tom']
    14 >>> names[0::2]  #2代表步长,每隔一个元素就取一个
    15 ['ZhangSan', 'WangWu', 'Tom']
    16 >>> names[::2]  #和上句效果一样
    17 ['ZhangSan', 'WangWu', 'Tom']
    18 >>>
    View Code

     追加操作:在列表最后追加元素列表

    1 >>> names=['ZhangSan','LiSi','WangWu']
    2 >>> names.append("我是新人")
    3 >>> names
    4 ['ZhangSan', 'LiSi', 'WangWu', '我是新人']
    5 >>>
    View Code

    插入操作

    1 >>> names=['ZhangSan','LiSi','WangWu','Tony']
    2 >>> names.insert(2,"Tom")  #在WangWu前面插入Tom
    3 >>> names
    4 ['ZhangSan', 'LiSi', 'Tom', 'WangWu', 'Tony']
    5 >>> names.insert(4,"LuXi")  #在WangWu后面插入LuXi
    6 >>> names
    7 ['ZhangSan', 'LiSi', 'Tom', 'WangWu', 'LuXi', 'Tony']
    8 >>>
    View Code

    修改操作

    1 >>> names=['ZhangSan','LiSi','WangWu','Tony']
    2 >>> names[2]="John" ##将WangWu改成"John"
    3 >>> names
    4 ['ZhangSan', 'LiSi', 'John', 'Tony']
    5 >>>
    View Code

    删除操作

     1 >>> names=['ZhangSan','LiSi','WangWu','ZhaoLiu','Tony','Tom','LiLei']
     2 >>> del names[2]  #删除下标为2的元素,即WangWu
     3 >>> names
     4 ['ZhangSan', 'LiSi', 'ZhaoLiu', 'Tony', 'Tom', 'LiLei']
     5 >>> del names[4]  #删除下标为4的元素,即Tom
     6 >>> names
     7 ['ZhangSan', 'LiSi', 'ZhaoLiu', 'Tony', 'LiLei']
     8 >>> names.remove("Tony")  #删除指定的元素Tony
     9 >>> names
    10 ['ZhangSan', 'LiSi', 'ZhaoLiu', 'LiLei']
    11 >>> names.pop()  #删除最后一个元素
    12 'LiLei'
    13 >>> names
    14 ['ZhangSan', 'LiSi', 'ZhaoLiu']
    15 >>>
    View Code

    扩展操作

    1 >>> names=['ZhangSan', 'LiSi', 'ZhaoLiu']
    2 >>> b=[1,2,3]
    3 >>> names.extend(b)
    4 >>> names
    5 ['ZhangSan', 'LiSi', 'ZhaoLiu', 1, 2, 3]
    6 >>>
    View Code

    统计列表中相同元素的个数

    1 >>> names=['ZhangSan', 'LiSi', 'ZhaoLiu','LiSi',1, 2, 3]
    2 >>> names.count("LiSi")
    3 2
    4 >>>
    View Code

    排序和翻转:优先级   特殊符号>数字>大写>小写

     1 >>> names=['ZhangSan', 'LiSi', 'ZhaoLiu','LiSi',1, 2, 3]
     2 >>> names.sort()
     3 Traceback (most recent call last):
     4   File "<stdin>", line 1, in <module>
     5 TypeError: unorderable types: int() < str()  #在python3里面不同数据类型不能在一起排序
     6 >>>
     7 >>> names[-1]='3'
     8 >>> names[-2]='2'
     9 >>> names[-3]='1'
    10 >>> names
    11 ['LiSi', 'LiSi', 'ZhangSan', 'ZhaoLiu', '1', '2', '3']
    12 >>> names.sort()  #将列表排序
    13 >>> names
    14 ['1', '2', '3', 'LiSi', 'LiSi', 'ZhangSan', 'ZhaoLiu']
    15 >>> names.reverse()  #将列表反转
    16 >>> names
    17 ['ZhaoLiu', 'ZhangSan', 'LiSi', 'LiSi', '3', '2', '1']
    18 >>>
    View Code

    获取下标

    1 >>> names=['ZhaoLiu', 'ZhangSan', 'LiSi', 'LiSi', '3', '2', '1']
    2 >>> names.index('LiSi')  #如果列表中有多个相同元素,只返回找到的第一个的下标
    3 2
    4 >>> names.index('3')
    5 4
    6 >>>
    View Code

    浅拷贝copy():

    浅复制并不会产生一个独立的对象单独存在,他只是将原有的数据块打上一个新标签,所以当其中一个标签被改变的时候,数据块就会发生变化,另一个标签也会随之改变。
     1 >>> names=['ZhaoLiu', 'ZhangSan',['lilei','luxi'], 'LiSi','3', '2', '1']
     2 >>> name_copy=names.copy()  #只是浅复制
     3 >>> names
     4 ['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
     5 >>> name_copy
     6 ['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
     7 >>> names[1]="张三"   #只改变names里面的值,name_copy里面的不变
     8 >>> names
     9 ['ZhaoLiu', '张三', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    10 >>> name_copy
    11 ['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    12 >>> names[2][0]="LILEI"  #names和name_copy里面的值都改变
    13 >>> names
    14 ['ZhaoLiu', '张三', ['LILEI', 'luxi'], 'LiSi', '3', '2', '1']
    15 >>> name_copy
    16 ['ZhaoLiu', 'ZhangSan', ['LILEI', 'luxi'], 'LiSi', '3', '2', '1']
    17 >>> name_copy[2][1]='露西'
    18 >>> names
    19 ['ZhaoLiu', '张三', ['LILEI', '露西'], 'LiSi', '3', '2', '1']
    20 >>> name_copy
    21 ['ZhaoLiu', 'ZhangSan', ['LILEI', '露西'], 'LiSi', '3', '2', '1']
    View Code

    深复制deepcopy():

    我们寻常意义的复制就是深复制,即将被复制对象完全再复制一遍作为独立的新个体单独存在。所以改变原有被复制对象不会对已经复制出来的新对象产生影响。
     1 #Author:Zhang XuLin
     2 import copy
     3 names=['ZhaoLiu', 'ZhangSan',['lilei','luxi'], 'LiSi','3', '2', '1']
     4 name_copy=copy.deepcopy(names)
     5 print(names)
     6 print(name_copy)
     7 print("========================")
     8 #['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
     9 #['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    10 #========================
    11 names[1]="张三"
    12 print(names)
    13 print(name_copy)
    14 print("========================")
    15 #['ZhaoLiu', '张三', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    16 #['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    17 #========================
    18 
    19 names[2][0]="LILEI"
    20 print(names)
    21 print(name_copy)
    22 print("************************************")
    23 #['ZhaoLiu', '张三', ['LILEI', 'luxi'], 'LiSi', '3', '2', '1']
    24 #['ZhaoLiu', 'ZhangSan', ['lilei', 'luxi'], 'LiSi', '3', '2', '1']
    25 #************************************
    26 
    27 name_copy[2][1]='露西'
    28 print(names)
    29 print(name_copy)
    30 
    31 #['ZhaoLiu', '张三', ['LILEI', 'luxi'], 'LiSi', '3', '2', '1']
    32 #['ZhaoLiu', 'ZhangSan', ['lilei', '露西'], 'LiSi', '3', '2', '1']
    View Code

     清空和删除列表

     1 >>> names=['ZhaoLiu', 'ZhangSan',['lilei','luxi'], 'LiSi','3', '2', '1']
     2 >>> names.clear()  #将列表清空
     3 >>> names
     4 []
     5 >>> del names  #将列表删除
     6 >>> names
     7 Traceback (most recent call last):
     8   File "<stdin>", line 1, in <module>
     9 NameError: name 'names' is not defined
    10 >>>

     

关键字