Python 常用方法
methods of List
index()
index() 函数用于从列表中找出某个值第一个匹配项的索引位置。
index() 方法语法:
list.index(x[, start[, end]])
获取第一个最简单了,list.index(x)
请确保元素x
在 list 中,否则会报错。
sort()
list.sort()
:将改变列表中的元素位置,返回值为 none,默认升序排列。
sorted(list)
:不改变列表元素原有顺序,返回排好序的列表
要自定排序方法,可以用 lambda 表达式
lst = [(2, 1, 3), (1, 2, 3), (4, 3, 2)]
lst.sort(key = lambda x: x[0], reverse = False )
list.sort(key = function, reverse = boolean)
本意是接受一个 function,这个 function 的参数是 list 这个“可迭代对象”中的对象,返回一个数作为比较的依据。
lst = [(2, 1, 3), (1, 2, 3), (4, 3, 2)]
def index2(item):
return item[2]
lst.sort(key = index2, reverse = False )
一定一定要把key = xxx, reverse = xxx
写上。
多依据排序装载自此。
对 tuple 进行排序,先按照第一个元素升序,如果第一个元素相同,再按照第二个元素降序排列。
L = [(12, 12), (34, 13), (32, 15), (12, 24), (32, 64), (32, 11)]
L.sort(key=lambda x: (x[0], -x[1]))
print(L)
列表的解析
[expr for iter_var in list if cond_expr]
按我个人理解:
[func(i) for i in list if filter(i)]
三个关键:expression、for-in、if
下面是样例:
n 个 0 的列表
可以利用 list 的解析生成一个含有 n 个 0 的 list
lst = [0 for i in range(n)]
字符串数组转 int 数组
[int(i) for i in s]
利用字典转换 key_list
这就是利用了列表的解析
dct = {'a':3, 'b':3,'c':5,'d':3}
key_list = ['c', 'd', 'a', 'b', 'd']
val_list = [dct[k] for k in key_list]
val_list = [5, 3, 3, 3, 3]
统计
可迭代对象(Iterable)有count()
方法,可以统计从指定位置开始对指定的对象进行计数
list.count(object,start,end)
,start 和 end 是可选参数
以统计 list 中元素出现个数并保存到字典中为例:
方法一
利用 collections 中的Counter
这个类的构造方法,再强转为 dictionary 类型
import collections
d = dict(collections.Counter(list))
方法二
dic = {}
for i in lst:
dic[i] = lst.count(i)
这样的缺点是遍历 lst 时会有重复,当然,也可以 set(list) 去重后再 count。
删除特定的元素
特别说明一个错误的方法:
for i in list_A:
if condition:
list_A.remove(i)
删除一个元素后,list 的长度变化,两个连续要被删除的元素,后面那个躲过去了,没被删除。
方法一
使用filter
这个类的构造函数,再将结果转换为 list 即可
def is_odd(n):
return n % 2 == 1
flt = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
newlist = list(flt)
print(newlist)
当然可以配合 lambda 函数
方法二
从列表的后面往前面删除特定的元素
方法三
遍历一个列表的拷贝,进行删除
for i in lst[:]:
if condition:
lst.remove(i)
合并
+
:如list_a + list_b
listA.extend(listB)
- Python-approaches-frequently-used approaches frequently Python usedpython-approaches-frequently-used python-approaches-frequently-used approaches decorators python how use generate python chart using frequently frequenter document replace python using frequently character functions numeric pdfplumber python using file approach