zoukankan      html  css  js  c++  java
  • python手册学习笔记2

    笔记2

    http://www.pythondoc.com/pythontutorial3/datastructures.html


    • 列表操作

    • list.append(x)

    把一个元素添加到列表的结尾,相当于 a[len(a):] = [x]

    • list.extend(L)

    将一个给定列表中的所有元素都添加到另一个列表中,相当于 a[len(a):] = L。

    • list.insert(i, x)

    在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引,例如 a.insert(0, x) 会插入到整个列表之前,而 a.insert(len(a), x) 相当于 a.append(x)

    • list.remove(x)

    删除列表中值为 x 的第一个元素。如果没有这样的元素,就会返回一个错误

    • list.pop([i])

    从列表的指定位置删除元素,并将其返回。如果没有指定索引,a.pop() 返回最后一个元素。元素随即从列表中被删除(方法中 i 两边的方括号表示这个参数是可选的,而不是要求你输入一对方括号,你会经常在Python 库参考手册中遇到这样的标记)

    • list.clear()

    从列表中删除所有元素。相当于 del a[:]

    • list.index(x)

    返回列表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。

    • list.count(x)

    返回 x 在列表中出现的次数。

    • list.sort()

    对列表中的元素就地进行排序。

    • list.reverse()

    就地倒排列表中的元素。

    • list.copy()

    返回列表的一个浅拷贝。等同于 a[:]

    >>> a = [66.25, 333, 333, 1, 1234.5]
    >>> print(a.count(333), a.count(66.25), a.count('x'))
    2 1 0
    >>> a.insert(2, -1)
    >>> a.append(333)
    >>> a
    [66.25, 333, -1, 333, 1, 1234.5, 333]
    >>> a.index(333)
    1
    >>> a.remove(333)
    >>> a
    [66.25, -1, 333, 1, 1234.5, 333]
    >>> a.reverse()
    >>> a
    [333, 1234.5, 1, 333, -1, 66.25]
    >>> a.sort()
    >>> a
    [-1, 1, 66.25, 333, 333, 1234.5]
    >>> a.pop()
    1234.5
    >>> a
    [-1, 1, 66.25, 333, 333]
    
    • 把列表当作堆栈使用

    用 append() 方法可以把一个元素添加到堆栈顶。用不指定索引的 pop() 方法可以把一个元素从堆栈顶释放出来

    >>> stack = [3, 4, 5]
    >>> stack.append(6)
    >>> stack.append(7)
    >>> stack
    [3, 4, 5, 6, 7]
    >>> stack.pop()
    7
    >>> stack
    [3, 4, 5, 6]
    >>> stack.pop()
    6
    >>> stack.pop()
    5
    >>> stack
    [3, 4]
    
    • 把列表当作队列使用

    collections 是 python 内建的一个集合模块,里面封装了许多集合类,其中队列相关的集合只有一个:deque。deque 是双边队列(double-ended queue),具有队列和栈的性质,在 list 的基础上增加了移动、旋转和增删等

    • d = collections.deque([])
    • d.append('a') # 在最右边添加一个元素~~,此时 d=deque('a')
    • d.appendleft('b') # 在最左边添加一个元素,此时d=deque(['b', 'a'])
    • d.extend(['c','d']) # 在最右边添加所有元素,此时d=deque(['b', 'a', 'c', 'd'])
    • d.extendleft(['e','f']) # 在最左边添加所有元素,此时 d=deque(['f', 'e', 'b', 'a', 'c', 'd'])
    • d.pop() # 将最右边的元素取出,返回 'd',此时 d=deque(['f', 'e', 'b', 'a', 'c'])
    • d.popleft() # 将最左边的元素取出,返回 'f',此时 d=deque(['e', 'b', 'a', 'c'])
    • d.rotate(-2) # 向左旋转两个位置(正数则向右旋转),此时 d=deque(['a', 'c', 'e', 'b'])
    • d.count('a') # 队列中'a'的个数,返回 1
    • d.remove('c') # 从队列中将'c'删除,此时 d=deque(['a', 'e', 'b'])
    • d.reverse() # 将队列倒序,此时 d=deque(['b', 'e', 'a'])
    from collections import deque
    a = [1,2,3,4,5,6]
    b = deque(a)
    b.appendleft(7)
    print(list(b))
    ...[7,1,2,3,4,5,6]
    
    • 列表生成式

    列表推导式为从序列中创建列表提供了一个简单的方法。普通的应用程式通过将一些操作应用于序列的每个成员并通过返回的元素创建列表,或者通过满足特定条件的元素创建子序列。

    >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
    ...[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
    

    矩阵倒转

    a = [
        [1,2,3],
        [4,5,6],
        [7,8,9]
    ]
    print([[x[i] for x in a]for i in range(3)])
    ...[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    
    • del语句

    有个方法可以从列表中按给定的索引而不是值来删除一个子项: del 语句。它不同于有返回值的 pop() 方法。语句 del 还可以从列表中删除切片或清空整个列表。例如:

    >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
    >>> del a[0]
    >>> a
    [1, 66.25, 333, 333, 1234.5]
    >>> del a[2:4]
    >>> a
    [1, 66.25, 1234.5]
    >>> del a[:]
    >>> a
    []
    >>> del a
    >>> a
    NameError: name 'a' is not defined
    
    • 使用zip()循环

    同时循环两个或更多的序列,可以使用 zip() 整体打包:

    key = [1,2,3,4]
    value = ['a','b','c','d']
    for k,v in zip(key,value):
        print(k,v)
    
    ...
    1 a
    2 b
    3 c
    4 d
    
    • 列表循环时更改列表内容

    若要在循环内部修改正在遍历的序列(例如复制某些元素),建议您首先制作副本。在序列上循环不会隐式地创建副本。切片表示法使这尤其方便:

    >>> words = ['cat', 'window', 'defenestrate']
    >>> for w in words[:]:  # Loop over a slice copy of the entire list.
    ...     if len(w) > 6:
    ...         words.insert(0, w)
    ...
    >>> words
    ['defenestrate', 'cat', 'window', 'defenestrate']
    
    作者:fengf233
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    解决VS控制台窗口自动关闭问题
    ZOJ1003:Crashing Balloon(dfs)
    POJ2607:Fire Station(SPFA+枚举)
    C语言在屏幕上输出玫瑰花图片
    HRBUST
    UVA10182: Bee Maja (模拟)
    洛谷P1144: 最短路计数(bfs)
    (转载)MySQL LIKE 用法:搜索匹配字段中的指定内容
    (转载)[MySQL技巧]INSERT INTO… ON DUPLICATE KEY UPDATE
    (转载)INSERT INTO .. ON DUPLICATE KEY 语法与实例教程
  • 原文地址:https://www.cnblogs.com/fengf233/p/10860809.html
Copyright © 2011-2022 走看看