Python 之内建模块整理

作者: 子龙0322 | 来源:发表于2019-02-18 17:14 被阅读32次

个人笔记、持续更新

Collections

  • from collections import Iterable:eg - isinstance([1, 3, 5], Iterable)
  • from collections import Iterator:能被 next() 执行的为 Iterator,为惰性计算。

random

  • random.choice(['apple', 'pear', 'banana']) -> 'apple'
  • random.sample(range(100), 10) -> [30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
  • random.random() -> random float
  • random.randrange(6) -> random integer chosen from range(6)

types:存储所以内置的 class 类型

  • types. FunctionType:自定义函数 class 类型
  • types. BuiltinFunctionType
    eg: type(abs)==types.BuiltinFunctionType -> True

os:与操作系统交互

  • os.listdir('.') -> 列出当前目录的文件名
  • os.getcwd() -> 返回当前文件的工作目录
  • os.chdir('/Users/mark/Desktop/...') -> 修改当前目录的工作环境
  • os.system('mkdir today') -> 执行系统命令
  • os.urandom(num) -> 产生 num 位 unicode 字符

shutil:提供常用的文件和目录操作方法

  • shutil.copyfile('data.db', 'archive.db')
  • shutil.move('/build/executables', 'installdir')

glob:从目录通配符搜索中生成文件列表

  • glob.glob('*.py') -> ['primes.py', 'random.py', 'quote.py']

sys:命令行参数

  • sys.argv:以 list 形式返回运行文件所在的目录
  • sys.path:以 list 形式返回

re:正则

  • re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') -> ['foot', 'fell', 'fastest']
  • re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') -> 'cat in the hat'

from datetime import date

  • date.today() -> '2019-01-28'
  • (date.today() - date(1991, 3, 22)).days -> 10174

zlib:数据压缩

  • zlib.compress(str) -> 压缩字符串
  • zlib.decompress(tStr) -> 解压缩字符串

相关文章

  • Python 之内建模块整理

    个人笔记、持续更新 Collections from collections import Iterable:eg...

  • Python 之内置函数整理

    个人笔记、持续更新 -- Built-in function import builtins Math abs(n...

  • pandas

    Pandas模块是Python用于数据导入及整理的模块 http://yam.gift/2017/02/15/li...

  • 2019-06-06 物品交易小网站构思

    答辩完成,想在这几天之内,完成这个功能模块。 完成前:new 技术 使用技术 python django reac...

  • Python常用模块

    Python常用模块之time模块 Python常用模块之os模块 Python常用模块之sys模块 Python...

  • [PyTorch]专项 输入-数据加载

    一、数据来源 原始数据来源:Kaggle python模块 二、数据整理 transforms() transfo...

  • python 2.7 -> python 3.7 升级记录

    更换的模块 python 3.7 模块名python 2.7 模块名python 3.7 包python 2.7包...

  • python

    With open函数打开文件的各种方式 Python 常用模块大全(整理)[https://www.cnblog...

  • Python学习笔记(一)

    近期整理一下暑期python学习的笔记,主要是一些基础知识 一.python程序的组成 1.python是模块由组...

  • 一些Python自带的常用模块

    原文python常用模块稍作整理 1.time模块 常用表示时间方式: 时间戳,格式化的时间字符串,元组(stru...

网友评论

    本文标题:Python 之内建模块整理

    本文链接:https://www.haomeiwen.com/subject/vpdcsqtx.html