笔记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']

最新文章

  1. 集合覆盖 顶点覆盖: set cover和vertex cover
  2. matlab如何建立一个空矩阵,然后往里面赋值
  3. [bzoj3694]最短路
  4. 【002: NetBeans 的 代码补全】
  5. 寻虫记:BOM头制造的冤案,无故多出空白行
  6. iOS开发之Xcode 如何使用API帮助
  7. java下载网络图片
  8. 单实例Singleton
  9. GridView.GridLines 属性
  10. Buenos Aires和Rio de Janeiro怎么发音?
  11. Android中帧动画的创建
  12. Struts2实现文件上传(二)
  13. JavaScript的作用域详解。
  14. linux下redis的安装方法
  15. 方程:方程(equation)是指含有未知数的等式
  16. Javaweb中提到的反射浅析(附源码)
  17. JS 自己实现Map
  18. zTree通过指定ID找到节点并选中
  19. Android中如何让DialogFragment全屏
  20. jenkins 判断是手动触发还是定时器触发

热门文章

  1. POJ - 2393Yogurt factory
  2. HashTable、Dictionary、ConcurrentDictionary三者区别
  3. 快速入门Maven(一)
  4. 不就是SELECT COUNT语句吗,竟然能被面试官虐的体无完肤
  5. win10-搭建git工具
  6. angular之模块开发二
  7. Qualcomm-Atheros-QCA9377-Wifi-Linux驱动
  8. 追查Could not get a databaseId from dataSource
  9. linux下安装配置svn服务器
  10. join的使用