zoukankan      html  css  js  c++  java
  • Python数据结构:列表

    • 列表的基本函数
    range(3) #小于3的整数:0,1,2
    range(1,10) #大于等于1小于10的整数,即[1,10)
    range(1,10,2)#大于等于1小于10的整数,步长为2,即1,3,5,7,9
    
    lst1 = [1,2,3,4]
    lst2 = ["zhangsan","lisi","wangwu"]
    len(lst1) #求数组的长度,输出为4
    max(lst1) #求数组的最大值
    min(lst1) #求数组的最小值
    st = "i love you"
    list(st) #转换为列表格式,结果为['i', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u']
    •  切片数组元素
    alist = ["zhangsan","lisi",1,2,3]
    alist[1:3] # 切片索引[1,3),结果为["lisi",1]
    alist[1:] # 切片索引1到最后,结果为["lisi",1,2,3]
    alist[1:-1] # 切片索引1到倒数第2个,-1表示最后一位,但是不包括,结果为["lisi",1,2]
    alist[1:-1:2] # 切片索引1到倒数第2个,步长为2,结果为["lisi",2]
    alist[2::-2] #从索引2开始,右向左切片,步长为负数表示从右向左取值,结果为[1,"zhangsan"]

    lst = [1,2,3,4,5,6,7]
    print(lst[-2:-4]) #输出为空
    print(lst[-4:-2]) #输出[4,5],默认情况只能从左至右切片,且包含左边不包含右边,数组的最后一个元素的索引为-1.到处第二个索引为-1,以此类推
    • id函数

    id函数的参数为需要查询的变量,返回值为该变量的id,id是变量的唯一标识

    a = 100
    b = 200
    print("id of a: {0}".format(id(a)))
    print("id pf b: {0}".format(id(b)))
    
    c = a
    print("id of c: {0}".format(id(c))) #c与a为同一个变量,id相同
    
    a= 101
    print("id of a: {0}".format(id(a))) #a一旦被修改,id发生变化
    print("id of c: {0}".format(id(c))) #c的id不变

    输出为

    id of a: 140707208945792
    id pf b: 140707208948992
    id of c: 140707208945792
    id of a: 140707208945824
    id of c: 140707208945792
    id of b: 140707208949024

    所有的分片操作是产生了一个新的数组,而不是在原数组上操作,原数组仍然保持不变。

    lst1 = [1,2,3]
    lst2 = lst1[:] #切片list所有元素
    lst3 = lst1 #lst3指向lst1的列表
    
    print(id(lst1))
    print(id(lst2)) #切片所得到的lst2与lst1的id不同
    print(id(lst3)) #lst3与lst1的id相同
    print("*"*20)
    
    lst1[1] = 100
    
    print(id(lst1))
    print(id(lst2)) #切片所得到的lst2与lst1的id不同
    print(id(lst3)) #lst1的第一个元素被修改,lst3和lst1的的id仍然保持一致
    print("*"*20)
    
    print(lst1)
    print(lst2) #lst1被修改,不影响lst2列表,lst2继续保持不变
    print(lst3) #lst1被修改,lst3也被修改,因为lst3和lst1指向同一个列表

    输出为

    2181593850440
    2181593850504
    2181593850440
    ********************
    2181593850440
    2181593850504
    2181593850440
    ********************
    [1, 100, 3]
    [1, 2, 3]
    [1, 100, 3]
    • 插入数组元素
    aList = ["zhangsan","lisi",1,2,3]
    bList = ["wangwu",4]
    aList.append(5) #在aList数组末尾加上元素5, 数组为["zhangsan","lisi",1,2,3,5]
    bList.insert(1,"xixi")  #在bList数组的索引为1的位置插入元素xixi,数组为["wangwu","xixi",4]
    aList.extend(bList) #将数组bList加入到数组aList中,aList为 ["zhangsan","lisi",1,2,3,"wangwu",4],bList不变
    • 删除数组元素
    alist=[1,2,3]
    blist=[“huawei”,"sanxing","xiaomi"]
    alist.pop(0) #删除数字第1个元素,删除后的数组为[2,3]
    alist.pop() #默认删除数字第1个元素,删除后的数组为[2,3]
    alist.remove("sanxing") #删除索元素"sanxing",删除后的数组为[“huawei”,"xiaomi"],此处的参数只能是元素值不能是索引,如果列表中没有这个数则报错
    del blist[2] #删除blist[2]元素,[]括号内必须是索引
    del blist #删除整个数组
    • 修改数组元素
    a = ["zhangsan","lisi",1,2,3]
    b = ["wangwu",4]
    a[1] = 5 #修改后的数组为["zhangsan",5,1,2,3]
    a[1:3] = ["update1","update2] #修改后的数组为["zhangsan","update1","update2",2,3]
    • 列表排序
    srt=["A","b","D","d"]
    srt.sort() #将数组按照ASCII码升序排列(默认即为升序),排序后的数组为['A', 'D', 'b', 'd']
    srt.sort(reverse=True) #将数组按照ASCII码降序排列,排序后的数组为['d', 'b', 'D', 'A']
    sorted(srt)#将srt数组排序,但是并没有改变srt数组本身的顺序,只是返回排序后的新数组
    sorted(srt,reverse=True)
    
    •  其他操作
    a = [1,2,3]
    b = ['a','b','c']
    c = a + b
    print(c) #a和b两个数组合并
    d = b * 3 #b数组扩大3倍
    print(d)

     输出为

    [1, 2, 3, 'a', 'b', 'c']
    ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
    1 in a #判断元素"A"是否在数组中,此处返回Ture
    "c" not in b#判断元素"d"是否不在数组中,此处返回False
    type(a) is list #判断对象srt是否为list类型,此处返回True
    •  嵌套列表/双层列表
    lst = [["one",1,"a"],["two",2,"b"],["three",3,"c"]]
    for k,v,w in lst:
        print(k,"...",v,"...",w)

    输出为

    one ... 1 ... a
    two ... 2 ... b
    three ... 3 ... c
    •  列表生成器,嵌套列表生成器
    # 使用列表a创建列表b
    a = [1,2,3]
    b = [i for i in a]
    print(a)
    print(b) #列表b和a的内容完全一致
    
    c = [i*10 for i in a] #对a中的每个元素乘以10,生成一个新的列表
    print(c)
    
    d = [x for x in range(1,10)] #生成一个1到9的列表
    print(d) #输出[1, 2, 3, 4, 5, 6, 7, 8, 9]
    e = [y for y in d if y%2==0] #取出列表d中的所有偶数生成新的列表
    print(e) #输出[2, 4, 6, 8]
    
    a = [1,2,3]
    b = [100,200]
    c = [i+j for i in a for j in b if i+j<200] #列表生成器可以嵌套,a在外层b在里层
    print(c) #输出[101, 102, 103]
    •  参数传递

     对于简单的数值(如int, float, string),采用传值,即在函数内的操作不影响函数外的该变量

     对于复杂的数值(如list, set, dict等),采用传址,即指向的是同一个地址,任何一个地方此内容的修改,都会影响其他地方的该变量

    def func1(lst):
        lst[0] = 101
        print("in func1, lst: ", lst)
        return None
    
    def func2(num):
        num += 100
        print(num)
        return None
    
    def func3(st):
        st = "updated"
        print("in func3, str: ", st)
        return None
    
    l = [1,2,3,4,5]
    print(l) #输出[1,2,3,4,5]
    func1(l) #传地址,而不是传值,因此,参数在函数内被修改,会体现到函数外
    print(l) #输出[101,2,3,4,5]
    
    n = 1
    print(n) #输出1
    func2(n) #传值,而不是传地址,因此,参数在函数内被修改,不会体现到函数外
    print(n) #输出1
    
    st = "initiated"
    print(st) #输出initiated
    func3(st) #传值,而不是传地址,因此,参数在函数内被修改,不会体现到函数外
    print(st) #输出initiated
    •  Copy函数
    a = [1,2,3,4,5]
    b = a #传地址,因此a和b指向同一个位置
    print(id(a),"-->",a)
    print(id(b),"-->",b)
    b[3]=100 #修改b的数据,同样a也会被修改
    print(a)
    print(b)
    
    c = a.copy() #传值,c是a的另一份拷贝,指向不同的位置
    print(id(a),"-->",a)
    print(id(c),"-->",c)
    c[1] = 200 #修改c的值,a不受影响
    print(a)
    print(c)

    输出为

    2428798001736 --> [1, 2, 3, 4, 5]
    [1, 2, 3, 100, 5]
    [1, 2, 3, 100, 5]
    2428798001736 --> [1, 2, 3, 100, 5]
    2428798492552 --> [1, 2, 3, 100, 5]
    [1, 2, 3, 100, 5]
    [1, 200, 3, 100, 5]

    深拷贝、浅拷贝,copy函数只浅拷贝函数,只拷贝一层内容:

    a = [1,2,3,[4,5,6]]
    b = a.copy()
    print(id(a))
    print(id(b)) #传值,a和b指向不同的空间
    print("*"*20)
    print(id(a[3]))
    print(id(b[3])) #a[3]和b[3]指向同一个空间,第二层内容不再拷贝,只传址
    
    a[3][0] = 100
    print(a[3])
    print(b[3]) #修改a[3]的任意值,b[3]也会被修改

     上述代码中的变量a相当于,a[3]相当于一个指针指向另外一个数组:

    输出为

    2056105648776
    2056106139464
    ********************
    2056105648712
    2056105648712
    [100, 5, 6]
    [100, 5, 6]
  • 相关阅读:
    tyvj 1031 热浪 最短路
    【bzoj2005】 [Noi2010]能量采集 数学结论(gcd)
    hdu 1394 Minimum Inversion Number 逆序数/树状数组
    HDU 1698 just a hook 线段树,区间定值,求和
    ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
    ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
    ZeptoLab Code Rush 2015 A. King of Thieves 暴力
    hdoj 5199 Gunner map
    hdoj 5198 Strange Class 水题
    vijos 1659 河蟹王国 线段树区间加、区间查询最大值
  • 原文地址:https://www.cnblogs.com/mryanzhao/p/9479952.html
Copyright © 2011-2022 走看看