字符串

.find()

  1. >>> 'ascd'.find('a')
  2. 0
  3. >>> 'ascd'.find('sc')
  4. 1

[: : -1]

  1. >>> a='python'
  2. >>> b=a[::-1]
  3. >>> print(b)
  4. nohtyp
  5. >>> c=a[::-2]
  6. >>> print(c)
  7. nhy
  8. >>> d = a[::2]
  9. >>> print(d)
  10. pto
  11. >>> e = a[1::2]
  12. >>> e
  13. yhn

lstrip(), rstrip()

截掉左、右边的字符,默认截掉空格

  1. >>> str = " this is string example....wow!!! ";
  2. >>> print str.lstrip();
  3. this is string example....wow!!!
  4. >>> str = "88888888this is string example....wow!!!8888888";
  5. >>> print str.lstrip('8');
  6. this is string example....wow!!!8888888

数组

zip()

zip返回的是一个迭代器,你不能用访问下标的方式去访问其中的某一个元素。

  1. >>>a = [1,2,3]
  2. >>> b = [4,5,6]
  3. >>> c = [4,5,6,7,8]
  4. >>> zipped = zip(a,b) # 打包为元组的列表
  5. [(1, 4), (2, 5), (3, 6)]
  6. >>> zip(a,c) # 元素个数与最短的列表一致
  7. [(1, 4), (2, 5), (3, 6)]
  8. >>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
  9. [(1, 2, 3), (4, 5, 6)]

enumerate()

  • 描述

enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。

  • 语法

enumerate(sequence, [start=0])
sequence — 一个序列、迭代器或其他支持迭代对象。
start — 下标起始位置。
返回 enumerate(枚举) 对象。

  • 实例

以下展示了使用 enumerate() 方法的实例:

  1. >>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
  2. >>> list(enumerate(seasons))
  3. [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
  4. >>> list(enumerate(seasons, start=1)) # 下标从 1 开始
  5. [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

普通的 for 循环

  1. >>> i = 0
  2. >>> seq = ['one', 'two', 'three']
  3. >>> for element in seq:
  4. ... print i, seq[i]
  5. ... i += 1
  6. ...
  7. 0 one
  8. 1 two
  9. 2 three

for 循环使用 enumerate

  1. >>> seq = ['one', 'two', 'three']
  2. >>> for i, element in enumerate(seq):
  3. ... print i, element
  4. ...
  5. 0 one
  6. 1 two
  7. 2 three

二维数组的创建

  1. demo1 = [[0]*3]*2
  2. demo2 = [[0 for i in range(3)]for j in range(2)]

image.png

不知道该写什么名字(

sum()

操作对象必须是可迭代的

  1. >>> sum(1,2)
  2. TypeError: 'int' object is not iterable

sort()、sorted()

  1. >>> a = (1,2,4,2,3) # a 是元组,故不能用sort() 排序
  2. >>> a.sort()
  3. AttributeError: 'tuple' object has no attribute 'sort'
  4. >>> sorted(a) # sorted() 可以为元组排序,返回一个新有序列表
  5. [1, 2, 2, 3, 4]
  6. >>> a=['1',1,'a',3,7,'n']
  7. >>> sorted(a)
  8. [1, 3, 7, '1', 'a', 'n']
  9. >>> a # sorted() 不改变原列表
  10. ['1', 1, 'a', 3, 7, 'n']
  11. >>> print a.sort()
  12. None
  13. >>> a # a.sort()直接修改原列表,返回值是none
  14. [1, 3, 7, '1', 'a', 'n']
  15. # 因此如果实际应用过程中需要保留原有列表,使用 sorted() 函数较为适合,
  16. # 否则可以选择 sort() 函数,因为 sort() 函数不需要复制原有列表,消耗的内存较少,效率也较高。
  17. # 对二维数组排序
  18. >>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) # 根据字典键排序
  19. [1, 2, 3, 4, 5]
  20. >>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}.values()) # 根据字典值排序
  21. ['A', 'B', 'B', 'D', 'E']
  22. # 对多维列表排序
  23. >>> student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10)]
  24. >>> sorted(student_tuples, key = lambda student: student[0]) # 对姓名排序
  25. [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
  26. >>> sorted(student_tuples, key = lambda student: student[2]) # 年龄排序
  27. [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
  28. # 调用operator模块中的 itemgetter() 可以实现根据多个参数排序:
  29. >>> sorted(student_tuples, key = itemgetter(2)) # 根据年龄排序
  30. [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
  31. >>> sorted(student_tuples, key = itemgetter(1, 2)) # 根据成绩和年龄排序
  32. [('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]
  33. >>> sorted(student_tuples, key = itemgetter(1, 2), reverse=True) # 反转排序结果
  34. [('jane', 'B', 12), ('dave', 'B', 10), ('john', 'A', 15)]
  35. ps: itemgetter 返回一个函数,实现取元素的功能。比如
  36. f = itemgetter(2),调用 f(r) 返回 r[2];
  37. f = itemgetter(2, 5, 3),调用 f(r) 返回元组 (r[2], r[5], r[3]).
  38. # 用某个自定义函数 cmp 作为比较相邻两个数大小的函数排序
  39. >>> from functools import cmp_to_key
  40. >>> nums.sort(key=cmp_to_key(lambda x, y: cmp(x,y)))

基础二维数组排序

  1. y = sorted(x, key = lambda x:(x[0],-x[1]))
  2. # 按照一维升序,二维降序
  3. 输入:[(264.0, 8, 0), (311.5, 10, 1), (230.0, 10, 2), (199.0, 9, 3)]
  4. 输出:[(199.0, 9, 3), (230.0, 10, 2), (264.0, 8, 0), (311.5, 10, 1)]

二维数组自定义排序

  1. import functools
  2. def comp(a,b):
  3. if b[0]-a[0]<=60:
  4. return b[1]-a[1]
  5. else:
  6. return a[0]-b[0]
  7. y = sorted(x, key = functools.cmp_to_key(comp))
  8. #当一维两元素之差在60之内时,按照二维降序,否则按一维升序排序。
  9. 输入:[(264.0, 8, 0), (311.5, 10, 1), (230.0, 10, 2), (199.0, 9, 3)]
  10. 输出:[(230.0, 10, 2), (199.0, 9, 3), (311.5, 10, 1), (264.0, 8, 0)]

gcd()

懂得都懂([

](https://blog.csdn.net/qq_34609519/article/details/107675410)