Python - 标准库(time,random,collections,itertools)

import time

  1. 获取现在时间
>>> import time
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=8, tm_mday=21, tm_hour=11, tm_min=20, tm_sec=59, tm_wday=4, tm_yday=234, tm_isdst=0)
>>> time.ctime()	# 返回字符串类类型的时间
'Fri Aug 21 11:21:07 2020'
  1. 时间戳与计时器
>>> time.time()	# 记录自纪元开始以来的秒数
1597980231.4828305
>>> time.perf_counter()	# 随意选取一个时间点,记录该事件到现在的秒数,不包含sleep
25.7903971
>>> time.process_time() # 随意选取一个时间点,记录该事件到现在的秒数,包含sleep
0.0625
  1. 格式化输出
>>> lctime = time.localtime()
>>> time.strftime("%Y-%m-%d %A %H:%M:%S", lctime)
'2020-08-21 Friday 15:21:59'
>>> time.strftime("%Y-%m-%d", lctime)
'2020-08-21'
>>> time.strftime("%H:%M:%S", lctime)
'15:21:59'
  1. 暂停
>>> time.sleep()

import random

  1. 随机种子
>>> from random import *
>>> seed(10)
>>> print(random())
0.5714025946899135
>>> print(random())
0.4288890546751146
>>> seed(10)
>>> print(random())
0.5714025946899135
  1. 产生随机整数
>>> randint(1,10)	# 取1-9之间的整数
6
>>> randrange(10)	# 取0-9之间的整数
7
>>> randrange(0,10,2)	# 取0,2,4,6,8; 其中的2为步幅
8
  1. 产生随机浮点数
>>> random()   # 0-1之间随机浮点数
0.014832446024553692
>>> uniform(0,10) # 0-10之间的随机浮点数
4.625805080929491
  1. 序列用函数
>>> choice(['a', 'b', 'c'])	# 对样本采样
'b'
>>> choice("python")	# 对字符串采样
't'
>>> choices(['a', 'b', 'c'], [4,4,2], k=20)	# 根据权重进行采样,可设置权重为None, k为采样次数
['b', 'a', 'b', 'a', 'a', 'c', 'c', 'a', 'c', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'c', 'c']

>>> num = ['1', '2', '3', '4'
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Python 3.9 版本的标准库列表: - abc - aifc - argparse - array - ast - asyncio - asyncore - atexit - audioop - base64 - bdb - binascii - binhex - bisect - builtins - bz2 - cProfile - calendar - cgi - cgitb - chunk - cmath - cmd - code - codecs - codeop - collections - collections.abc - colorsys - compileall - concurrent - configparser - contextlib - contextvars - copy - copyreg - crypt - csv - ctypes - curses - dataclasses - datetime - dbm - decimal - difflib - dis - distutils - doctest - email - encodings - ensurepip - enum - errno - faulthandler - fcntl - filecmp - fileinput - fnmatch - formatter - fractions - ftplib - functools - gc - getopt - getpass - glob - grp - gzip - hashlib - heapq - hmac - html - http - hyperlinks - imaplib - imghdr - importlib - inspect - io - ipaddress - itertools - json - keyword - lib2to3 - linecache - locale - logging - lzma - mailbox - mailcap - marshal - math - mmap - modulefinder - msilib - multiprocessing - netrc - nis - nntplib - numbers - opcode - operator - optparse - os - os.path - parser - pathlib - pdb - pickle - pickletools - pipes - pkgutil - platform - plistlib - poplib - pprint - profile - pstats - pty - py_compile - queue - quopri - random - re - readline - reprlib - resource - rlcompleter - runpy - sched - secrets - select - selectors - shelve - shlex - shutil - signal - site - smtpd - smtplib - sndhdr - socket - socketserver - sqlite3 - ssl - stat - statistics - string - stringprep - struct - subprocess - sunau - symbol - symtable - sys - sysconfig - syslog - tabnanny - tarfile - telnetlib - tempfile - termios - test - textwrap - threading - time - timeit - tkinter - token - tokenize - traceback - tracemalloc - tty - turtle - types - typing - unicodedata - unittest - urllib - uu - uuid - venv - warnings - wave - weakref - webbrowser - winreg - winsound - wsgiref - xdrlib - xml - xmlrpc - zipapp - zipfile - zipimport - zlib

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值