字符串
.find()
>>> 'ascd'.find('a')0>>> 'ascd'.find('sc')1
[: : -1]
>>> a='python'>>> b=a[::-1]>>> print(b)nohtyp>>> c=a[::-2]>>> print(c)nhy>>> d = a[::2]>>> print(d)pto>>> e = a[1::2]>>> eyhn
lstrip(), rstrip()
截掉左、右边的字符,默认截掉空格
>>> str = " this is string example....wow!!! ";>>> print str.lstrip();this is string example....wow!!!>>> str = "88888888this is string example....wow!!!8888888";>>> print str.lstrip('8');this is string example....wow!!!8888888
数组
zip()
zip返回的是一个迭代器,你不能用访问下标的方式去访问其中的某一个元素。
>>>a = [1,2,3]>>> b = [4,5,6]>>> c = [4,5,6,7,8]>>> zipped = zip(a,b) # 打包为元组的列表[(1, 4), (2, 5), (3, 6)]>>> zip(a,c) # 元素个数与最短的列表一致[(1, 4), (2, 5), (3, 6)]>>> zip(*zipped) # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式[(1, 2, 3), (4, 5, 6)]
enumerate()
- 描述
enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。
- 语法
enumerate(sequence, [start=0])
sequence — 一个序列、迭代器或其他支持迭代对象。
start — 下标起始位置。
返回 enumerate(枚举) 对象。
- 实例
以下展示了使用 enumerate() 方法的实例:
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']>>> list(enumerate(seasons))[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]>>> list(enumerate(seasons, start=1)) # 下标从 1 开始[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
普通的 for 循环
>>> i = 0>>> seq = ['one', 'two', 'three']>>> for element in seq:... print i, seq[i]... i += 1...0 one1 two2 three
for 循环使用 enumerate
>>> seq = ['one', 'two', 'three']>>> for i, element in enumerate(seq):... print i, element...0 one1 two2 three
二维数组的创建
demo1 = [[0]*3]*2demo2 = [[0 for i in range(3)]for j in range(2)]
不知道该写什么名字(
sum()
操作对象必须是可迭代的
>>> sum(1,2)TypeError: 'int' object is not iterable
sort()、sorted()
>>> a = (1,2,4,2,3) # a 是元组,故不能用sort() 排序>>> a.sort()AttributeError: 'tuple' object has no attribute 'sort'>>> sorted(a) # sorted() 可以为元组排序,返回一个新有序列表[1, 2, 2, 3, 4]>>> a=['1',1,'a',3,7,'n']>>> sorted(a)[1, 3, 7, '1', 'a', 'n']>>> a # sorted() 不改变原列表['1', 1, 'a', 3, 7, 'n']>>> print a.sort()None>>> a # a.sort()直接修改原列表,返回值是none[1, 3, 7, '1', 'a', 'n']# 因此如果实际应用过程中需要保留原有列表,使用 sorted() 函数较为适合,# 否则可以选择 sort() 函数,因为 sort() 函数不需要复制原有列表,消耗的内存较少,效率也较高。# 对二维数组排序>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) # 根据字典键排序[1, 2, 3, 4, 5]>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}.values()) # 根据字典值排序['A', 'B', 'B', 'D', 'E']# 对多维列表排序>>> student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10)]>>> sorted(student_tuples, key = lambda student: student[0]) # 对姓名排序[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]>>> sorted(student_tuples, key = lambda student: student[2]) # 年龄排序[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]# 调用operator模块中的 itemgetter() 可以实现根据多个参数排序:>>> sorted(student_tuples, key = itemgetter(2)) # 根据年龄排序[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]>>> sorted(student_tuples, key = itemgetter(1, 2)) # 根据成绩和年龄排序[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]>>> sorted(student_tuples, key = itemgetter(1, 2), reverse=True) # 反转排序结果[('jane', 'B', 12), ('dave', 'B', 10), ('john', 'A', 15)]ps: itemgetter 返回一个函数,实现取元素的功能。比如f = itemgetter(2),调用 f(r) 返回 r[2];f = itemgetter(2, 5, 3),调用 f(r) 返回元组 (r[2], r[5], r[3]).# 用某个自定义函数 cmp 作为比较相邻两个数大小的函数排序>>> from functools import cmp_to_key>>> nums.sort(key=cmp_to_key(lambda x, y: cmp(x,y)))
基础二维数组排序
y = sorted(x, key = lambda x:(x[0],-x[1]))# 按照一维升序,二维降序输入:[(264.0, 8, 0), (311.5, 10, 1), (230.0, 10, 2), (199.0, 9, 3)]输出:[(199.0, 9, 3), (230.0, 10, 2), (264.0, 8, 0), (311.5, 10, 1)]
二维数组自定义排序
import functoolsdef comp(a,b):if b[0]-a[0]<=60:return b[1]-a[1]else:return a[0]-b[0]y = sorted(x, key = functools.cmp_to_key(comp))#当一维两元素之差在60之内时,按照二维降序,否则按一维升序排序。输入:[(264.0, 8, 0), (311.5, 10, 1), (230.0, 10, 2), (199.0, 9, 3)]输出:[(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)
