本节对一些 python 易混淆的操作进行对比。
1.1 有放回随机采样和无放回随机采样
import randomrandom.choices(seq, k=1) # 长度为k的list,有放回采样random.sample(seq, k) # 长度为k的list,无放回采样
1.2 lambda 函数的参数
func = lambda y: x + y # x的值在函数运行时被绑定func = lambda y, x=x: x + y # x的值在函数定义时被绑定
1.3 copy 和 deepcopy
import copyy = copy.copy(x) # 只复制最顶层y = copy.deepcopy(x) # 复制所有嵌套部分
复制和变量别名结合在一起时,容易混淆:
a = [1, 2, [3, 4]]# alias.b_alias = a assert b_alias == a and b_alias is a# shallow copy.b_shallow_copy = a[:] assert b_shallow_copy == a and b_shallow_copy is not a and b_shallow_copy[2] is a[2]# deep copy.import copyb_deep_copy = copy.deepcopy(a) assert b_deep_copy == a and b_deep_copy is not a and b_deep_copy[2] is not a[2]
对别名的修改会影响原变量,(浅)复制中的元素是原列表中元素的别名,而深层复制是递归的进行复制,对深层复制的修改不影响原变量。
2、常用工具
2.1 读写 csv 文件
import csv# 无header的读写with open(name, 'rt', encoding='utf-8', newline='') as f: # newline=''让python不将换行统一处理 for row in csv.reader(f): print(row[0], row[1]) # csv读到的数据都是str类型with open(name, mode='wt') as f: f_csv = csv.writer(f) f_csv.writerow(['symbol', 'change'])# 有header的读写with open(name, mode='rt', newline='') as f: for row in csv.dictreader(f): print(row['symbol'], row['change'])with open(name, mode='wt') as f: header = ['symbol', 'change'] f_csv = csv.dictwriter(f, header) f_csv.writeheader() f_csv.writerow({'symbol': xx, 'change': xx})
注意,当 csv 文件过大时会报错:_csv.error: field larger than field limit (131072),通过修改上限解决
import syscsv.field_size_limit(sys.maxsize)
csv 还可以读以 \t 分割的数据
f = csv.reader(f, delimiter='\t')
2.2 迭代器工具
itertools 中定义了很多迭代器工具,例如子序列工具:
import itertoolsitertools.islice(iterable, start=none, stop, step=none)# islice('abcdef', 2, none) -> c, d, e, fitertools.filterfalse(predicate, iterable) # 过滤掉predicate为false的元素# filterfalse(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 6itertools.takewhile(predicate, iterable) # 当predicate为false时停止迭代# takewhile(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 1, 4itertools.dropwhile(predicate, iterable) # 当predicate为false时开始迭代# dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1]) -> 6, 4, 1itertools.compress(iterable, selectors) # 根据selectors每个元素是true或false进行选择# compress('abcdef', [1, 0, 1, 0, 1, 1]) -> a, c, e, f
序列排序:
sorted(iterable, key=none, reverse=false)itertools.groupby(iterable, key=none) # 按值分组,iterable需要先被排序# groupby(sorted([1, 4, 6, 4, 1])) -> (1, iter1), (4, iter4), (6, iter6)itertools.permutations(iterable, r=none) # 排列,返回值是tuple# permutations('abcd', 2) -> ab, ac, ad, ba, bc, bd, ca, cb, cd, da, db, dcitertools.combinations(iterable, r=none) # 组合,返回值是tupleitertools.combinations_with_replacement(...)# combinations('abcd', 2) -> ab, ac, ad, bc, bd, cd
多个序列合并:
itertools.chain(*iterables) # 多个序列直接拼接# chain('abc', 'def') -> a, b, c, d, e, fimport heapqheapq.merge(*iterables, key=none, reverse=false) # 多个序列按顺序拼接# merge('abf', 'cde') -> a, b, c, d, e, fzip(*iterables) # 当最短的序列耗尽时停止,结果只能被消耗一次itertools.zip_longest(*iterables, fillvalue=none) # 当最长的序列耗尽时停止,结果只能被消耗一次
2.3 计数器
计数器可以统计一个可迭代对象中每个元素出现的次数。
import collections# 创建collections.counter(iterable)# 频次collections.counter[key] # key出现频次# 返回n个出现频次最高的元素和其对应出现频次,如果n为none,返回所有元素collections.counter.most_common(n=none)# 插入/更新collections.counter.update(iterable)counter1 + counter2; counter1 - counter2 # counter加减# 检查两个字符串的组成元素是否相同collections.counter(list1) == collections.counter(list2)
2.4 带默认值的 dict
当访问不存在的 key 时,defaultdict 会将其设置为某个默认值。
import collectionscollections.defaultdict(type) # 当第一次访问dict[key]时,会无参数调用type,给dict[key]提供一个初始值
2.5 有序 dict
import collectionscollections.ordereddict(items=none) # 迭代时保留原始插入顺序
3、高性能编程和调试
3.1 输出错误和警告信息
向标准错误输出信息
import syssys.stderr.write('')
输出警告信息
import warningswarnings.warn(message, category=userwarning) # category的取值有deprecationwarning, syntaxwarning, runtimewarning, resourcewarning, futurewarning
控制警告消息的输出
$ python -w all # 输出所有警告,等同于设置warnings.simplefilter('always')$ python -w ignore # 忽略所有警告,等同于设置warnings.simplefilter('ignore')$ python -w error # 将所有警告转换为异常,等同于设置warnings.simplefilter('error')
3.2 代码中测试
有时为了调试,我们想在代码中加一些代码,通常是一些 print 语句,可以写为:
# 在代码中的debug部分if __debug__: pass
一旦调试结束,通过在命令行执行 -o 选项,会忽略这部分代码:
$ python -0 main.py
3.3 代码风格检查
使用 pylint 可以进行不少的代码风格和语法检查,能在运行之前发现一些错误
pylint main.py
3.4 代码耗时
耗时测试
$ python -m cprofile main.py
测试某代码块耗时
# 代码块耗时定义from contextlib import contextmanagerfrom time import perf_counter@contextmanagerdef timeblock(label): tic = perf_counter() try: yield finally: toc = perf_counter() print('%s : %s' % (label, toc - tic))# 代码块耗时测试with timeblock('counting'): pass
代码耗时优化的一些原则
专注于优化产生性能瓶颈的地方,而不是全部代码。避免使用全局变量。局部变量的查找比全局变量更快,将全局变量的代码定义在函数中运行通常会快 15%-30%。避免使用.访问属性。使用 from module import name 会更快,将频繁访问的类的成员变量 self.member 放入到一个局部变量中。尽量使用内置数据结构。str, list, set, dict 等使用 c 实现,运行起来很快。避免创建没有必要的中间变量,和 copy.deepcopy()。字符串拼接,例如 a + ':' + b + ':' + c 会创造大量无用的中间变量,':',join([a, b, c]) 效率会高不少。另外需要考虑字符串拼接是否必要,例如 print(':'.join([a, b, c])) 效率比 print(a, b, c, sep=':') 低。
以上就是20个python使用小技巧,建议收藏!的详细内容。
