zoukankan      html  css  js  c++  java
  • python 列表

    任意对象的有序集合

    通过偏移读取

    可变长度,异构以及嵌套

    可变序列

    对象引用数组

    l = [1,2,3]
    print(len(l)) # 3
    print([1,2,4] + [5,6,7]) # [1, 2, 4, 5, 6, 7]
    print(['hi'] *4) # ['hi', 'hi', 'hi', 'hi']
    print([1,2] + list('34')) # [1, 2, '3', '4']
    print(3 in [1,2,3]) # True

    列表解析

    for i in [1,2,3]:
    print(i,end='') # 123
    print([x * 4 for x in [1,2,3]]) # [4, 8, 12]
    l = []
    for i in [1,2,3]:
    l.append(i *4)
    print(l) # [4, 8, 12]
    print(list(map(abs,[1,-2,-3,-4]))) # [1, 2, 3, 4]

    索引,分片,矩阵

    l = ['spam','Spam','SPAM']
    print(l[0]) # spam
    print(l[-2]) # Spam
    print(l[1:]) # ['Spam', 'SPAM']
    l = [[1,2,3],[4,5,6],[7,8,9]]
    print(l[1]) # [4, 5, 6]
    l = [1,2,3]
    l[0] = 'spam'
    print(l) # ['spam', 2, 3]
    l[:2] = ['eat','more']
    print(l) # ['eat', 'more', 3]
    l[2] = [4,5]
    print(l) # ['eat', 'more', [4, 5]]

    列表方法调用

    l.append('lux')
    print(l) # ['eat', 'more', [4, 5], 'lux']
    l =[3,6,3,2,3,4,5,6]
    l.sort()
    print(l) # [2, 3, 3, 3, 4, 5, 6, 6]
    l = ['abc','SDF','Dcf']
    l.sort(key = str.lower)
    print(l) # ['abc', 'Dcf', 'SDF']
    l.sort(key = str.lower,reverse = True)
    print(l) # ['SDF', 'Dcf', 'abc']
    l = ['abc','SDF','Dcf']
    print(sorted(l,key = str.lower,reverse = True)) #['SDF', 'Dcf', 'abc']
    l = ['abc','SDF','Dcf']
    print(sorted([x.lower() for x in l], reverse = True)) # ['sdf', 'dcf', 'abc']
    l = [1,2]
    l.extend([3,4,5])
    print(l) # [1, 2, 3, 4, 5]
    print(l.pop()) # 5
    l.reverse()
    print(l) # [4, 3, 2, 1]
    print(list(reversed(l))) # [1, 2, 3, 4]
    l = []
    l.append(1)
    l.append(2)
    print(l) # [1, 2]
    print(l.pop()) # 2
    print(l.pop()) # 1
    print(l) # []
    l = ['abc','SDF','Dcf']
    print(l.index('abc')) # 0
    l.insert(1,'ddd')
    print(l) # ['abc', 'ddd', 'SDF', 'Dcf']
    l.remove('ddd')
    print(l) # ['abc', 'SDF', 'Dcf']
    l.pop(1)
    print(l) # ['abc', 'Dcf']
    l = ['abc','SDF','Dcf']
    del l[0]
    print(l) # ['SDF', 'Dcf']
    del l[1:]
    print(l) # ['SDF']
    l = ['abc','SDF','Dcf']
    l[1:] = []
    print(l) # ['abc']
    l[0] = []
    print(l) # [[]]

  • 相关阅读:
    JDBC_查询
    微信web小程序开发1
    小型人口普查系统2
    小型人口普查系统1
    JSP内置对象作用域
    Session和Cookie的区别
    Cookie
    Session
    c++中嵌入python
    目标文件obj的各段 2
  • 原文地址:https://www.cnblogs.com/jibandefeng/p/11160284.html
Copyright © 2011-2022 走看看