04 2020 档案
摘要:源码解读 BaseSerializer(field) def __init__(self, instance=None, data=empty, **kwargs): self.instance = instance if data is not empty: self.initial_data =
阅读全文
摘要:将输出写入文件 with open('demo1.py', 'wt', encoding='utf8') as f: print('hello', file=f) 使用其他分隔符和换行符打印 print('ACME', 50, 91.5) print('ACME', 50, 91.5, sep=',
阅读全文
摘要:fill 将多行文本按照指定的宽度和格式输出 s = "Look into my eyes, look into my eyes, the eyes, the eyes, \ the eyes, not around the eyes, don't look around the eyes, \ l
阅读全文
摘要:fnmatch() 函数匹配能力介于简单的字符串方法和强大的正则表达式之间。 如果在数据处理操作中只需要简单的通配符就能完成的时候,这通常是一个比较合理的方案 import os from fnmatch import fnmatchcase files = os.listdir('.') py_f
阅读全文
摘要:itemgetter 字典列表进行排序 rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John',
阅读全文
摘要:defaultdict 创建一个多值字典,如果我们自己实现 # 我们自己实现 d = {} for key, value in pairs: if key not in d: d[key] = [] d[key].append(value) # 使用defaultdict d = defaultdi
阅读全文
摘要:当要查找的元素个数相对比较小的时候,函数 nlargest() 和 nsmallest() 是很合适的。 如果你仅仅想查找唯一的最小或最大(N=1)的元素的话,那么使用 min() 和 max() 函数会更快些。 类似的,如果 N 的大小和集合大小接近的时候,通常先排序这个集合然后再使用切片操作会更
阅读全文
摘要:任意可迭代对象都可以进行解包操作 字符串 元组 列表 文件对象 迭代器和生成器 原则: 赋值变量的个数和可迭代对象的个数保持一致 字符串 name = 'wangys' a, b, c, d, e, f = name print(a) print(b) print(c) print(d) print
阅读全文