zoukankan      html  css  js  c++  java
  • python学习笔记——列表

    创建

    >>> list = ["a", "b", "c", "d", "e"]
    >>> list
    ['a', 'b', 'c', 'd', 'e']
    

    获取某一元素或子串

    >>> list = ["a", "b", "c", "d", "e"]
    #取得第一个元素
    >>> list[0]
    'a'
    
    # 取得最后一个元素
    >>> list[-1]
    'e'
    # 若索引值超出列表长度之外,就会报错
    >>> list[10]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range
    
    # 使用切片,模拟javascript的slice功能,获取子串
    >>> list[1:4]
    ['b', 'c', 'd']
    
    # 切片的起始索引默认为0
    >>> list[:3]
    ['a', 'b', 'c']
    
    # 切片的起始索引默认为-1
    >>> list[3:]
    ['d', 'e']
    
    # 超出范围会自动修正为列表的长度
    >>> list[3:10]
    ['d', 'e']
    

    替换

    >>> list = ["a", "b", "c", "d", "e"]
    
    # 替换单个元素
    >>> list[3] = "D"
    >>> list
    ['a', 'b', 'c', 'D', 'e']
    
    # 替换N个元素
    >>> list[:3] = ["A", "B", "C"]
    >>> list
    ['A', 'B', 'C', 'D', e]
    
    # 通换替换进行扩展,有点像javascript的splice
    >>> list[-1:] = ["E", "F"]
    >>> list
    ['A', 'B', 'C', 'D', 'E', 'F']
    
    # 通换替换进行收缩
    >>> list[-2:] = "E"
    >>> list
    ['A', 'B', 'C', 'D', 'E']
    

    追加

    >>> list = ['b'] # push一个元素,注意此方法没有返回值 >>> list.append('e') >>> list >>> ['b', 'e'] # 在指定位置上添加元素 >>> list.insert(0, 'a') >>> list ['a', 'b', 'e'] # 在指定位置上添加多个无形 >>> list[2:2] = ['c', 'd'] >>> list ['a', 'b', 'c', 'd', 'e']

    合并

    >>> list1 = ['a', 'b', 'c']
    >>> list2 = ['d', 'e']
    
    #  使用+号操作符生成一个全新的列表
    >>> list3 = list1 + list2
    >>> list3
    ['a', 'b', 'c', 'd', 'e']
    >>> list1
    ['a', 'b', 'c']
    
    # 使用extend在原列表上进行扩展,注意此方法没有返回值
    >>> list1.extend(list2)
    >>> list1
    ['a', 'b', 'c', 'd', 'e']
    

    检测

    
    >>> list = ["a", "b", "c", "a", "b"]
    
    # 判定其是否为目标列表的成员
    >>> "b" in list
    True
    >>> "e" in list
    False
    
    # 使用index方法还可以取得其在列表的位置
    >>> list.index("a")
    0
    
    # 但是找不到时会报错,shit,什么破设计
    >>> list.index("e")
    Traceback (most recent call last):
      File "<stdin>", line 1, in 
    ValueError: list.index(x): x not in list
    

    删除

    
    >>> list = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
    
    # 类似javascript的pop函数
    >>> list.pop()
    'b'
    >>> list
    ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']
    
    # 可以通过传参,实现javascript的shift功能
    >>> list.pop(0)
    'a'
    >>> list
    ['a', 'a', 'b', 'b', 'b', 'c', 'c']
    
    # 指定移除的元素,每次只移除1个
    >>> list.remove('b')
    >>> list
    ['a', 'a', 'b', 'b', 'c', 'c']
    
    # 但如果不存在就会报错,这个非常不人性化
    >>> list.remove(10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in 
    ValueError: list.remove(x): x not in list
    
    # 清空所有‘b’元素
    >>> while 'b' in list: list.remove('b')
    ...
    >>> list
    ['a', 'a', 'c', 'c']
    
    # 移除一组元素
    >>> del list[1:3] #与 list[1:3] = [] 相同
    >>> list
    ['a', 'c']
    
    # 全部清空
    >>> del list[:] #与 list[:] = [] 相同
    >>> list
    []
    

    统计

    
    >>> list = ['a', 'a', 'b', 'c']
    
    >>> len(list)
    4
    
    # 返回a在列表出现的次数
    >>> list.count('a')
    2
    

    列表解析

    
    >>> S = [x**2 for x in range(10)]
    >>> V = [2**i for i in range(13)]
    >>> M = [x for x in S if x % 2 == 0]
    >>> 
    >>> print S; print V; print M
    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
    [0, 4, 16, 36, 64]
    
    >>> noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
    >>> primes = [x for x in range(2, 50) if x not in noprimes]
    >>> print primes
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    
    >>> words = 'The quick brown fox jumps over the lazy dog'.split()
    >>> print words
    ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
    >>> 
    >>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]
    >>> for i in stuff:
    ...     print i
    ... 
    ['THE', 'the', 3]
    ['QUICK', 'quick', 5]
    ['BROWN', 'brown', 5]
    ['FOX', 'fox', 3]
    ['JUMPS', 'jumps', 5]
    ['OVER', 'over', 4]
    ['THE', 'the', 3]
    ['LAZY', 'lazy', 4]
    ['DOG', 'dog', 3]
    >>> 
    >>> stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)
    >>> for i in stuff:
    ...     print i
    ... 
    ['THE', 'the', 3]
    ['QUICK', 'quick', 5]
    ['BROWN', 'brown', 5]
    ['FOX', 'fox', 3]
    ['JUMPS', 'jumps', 5]
    ['OVER', 'over', 4]
    ['THE', 'the', 3]
    ['LAZY', 'lazy', 4]
    ['DOG', 'dog', 3]
    

    将一个二维列表平坦化

    >>> list = [[1, 3, 5], [2, 4]]
    >>> [flatten for inner in list for flatten in inner]
    [1, 3, 5, 2, 4]
    
    
  • 相关阅读:
    链表的相关操作
    【回溯】旅行商问题
    【回溯】图的m着色问题
    奶牛卧室_待解决
    阶乘问题
    子数整数
    hihoCoder week4 Trie图
    hihoCoder week3 KMP算法
    hihoCoder week2 Trie树
    hihoCoder week1 最长回文子串
  • 原文地址:https://www.cnblogs.com/rubylouvre/p/2084497.html
Copyright © 2011-2022 走看看