9.python标准库

╰半橙微兮° 2023-06-24 08:06 50阅读 0赞
  1. #-*- coding: utf-8 -*-
  2. print("================time标准库================")
  3. #time库 python处理时间的标准库
  4. #获取现在时间 time.localtime()
  5. #获取UTC世界统一时间 time.gmtime 北京时间比统一时间早8个小时
  6. import time
  7. t_local = time.localtime()
  8. t_UTC = time.gmtime()
  9. print("t_local",t_local) #本地时间
  10. print("t_UTC",t_UTC) #UTC统一时间
  11. print(time.ctime()) #返回本地时间的字符串
  12. #时间戳与计时器
  13. #time.time() 返回自纪元以来的秒数,记录sleep
  14. #time.perf_counter() 随意选取一个时间点,记录现在时间到该时间点间隔秒数,记录 sleep
  15. #time.process_time() 随意选取一个时间点,记录现在时间到该时间点间隔秒数,不记录 sleep
  16. #perf_counter()精度比time()更高一些
  17. t_1_start = time.time()
  18. t_2_start = time.perf_counter()
  19. t_3_start = time.process_time()
  20. print(t_1_start)
  21. print(t_2_start)
  22. print(t_3_start)
  23. #时间格式化输出 time.triftime 自定义格式化输出
  24. lctime = time.localtime()
  25. print(time.strftime("%Y-%m-%d %A %H:%M:%S",lctime))
  26. #睡眠
  27. time.sleep(1)
  28. print("================random标准库==============")
  29. #python通过random库提供各种伪随机数
  30. #随机种子 -- seed(a=None)
  31. #相同种子会产生相同随机数,如果不设置随机种子,以系统当前时间为默认值
  32. from random import *
  33. seed(10)
  34. print(random())
  35. seed(10)
  36. print(random())
  37. #产生随机整数
  38. #randint(a,b) -- 产生[a,b]之间的随机整数
  39. numbers = [randint(1,10) for i in range(10)]
  40. print(numbers)
  41. #randrange(a) -- 产生[0,a)之间的随机整数,不包含a
  42. numbers = [randrange(10) for i in range(10)]
  43. print(numbers)
  44. #randrange(a,b,step) -- 产生[a,b)之间以step为步长的随机整数
  45. numbers = [randrange(0,10,2) for i in range(10)]
  46. print(numbers)
  47. #产生随机浮点数
  48. #random()--产生[0.0,1.0]之间的随机浮点数
  49. numbers = [random() for i in range(10)]
  50. print(numbers)
  51. #uniform(a,b)-- 产生[a,b]之间的随机浮点数
  52. numbers = [uniform(2.1,3.5) for i in range(10)]
  53. print(numbers)
  54. #序列用函数
  55. #choice(seq)--从序列类型中随机返回一个元素
  56. print(choice(['win','lose','draw']))
  57. print("python")
  58. #choices(seq,weights=None,k) --对序列类型进行k次重复采样,可设置权重
  59. #重复采样5次,取完之后放回再取
  60. print(choices(['win','lose','draw'],k=5))
  61. #可对每个元素设置权重
  62. print(choices(['win','lose','draw'],[4,4,2],k=10))
  63. #shuffle(seq) 将序列类型中元素随机排列,返回打乱后对序列
  64. numbers = ["one","two","three","four"]
  65. shuffle(numbers)
  66. print(numbers)
  67. #sample(pop,k) -- 从pop类型中随机取k个元素,以列表类型返回
  68. print(sample([10,20,30,40,50],k=3))
  69. #概率分布 以高斯分布为例
  70. #gauss(mean,std) 生产一个符合高斯分布的随机数 mean是均值 std是标准差
  71. number = gauss(0,1)
  72. print(number)
  73. #多生成几个画直方图显示
  74. import matplotlib.pyplot as plt
  75. res = [gauss(0,1) for i in range(100000)]
  76. plt.hist(res,bins=1000)
  77. #plt.show()
  78. print("================collections标准库=========")
  79. #提供容器数据类型,可以看作之前学习过的组合数据类型的扩展
  80. #namedtuple,具名元组,即有名字的元组
  81. #构建一个新的元组子类,定义方法如下:typename是元组名字,field_names是域名, 即元组里面要存的元素的名字
  82. import collections
  83. #collections.namedtuple(typename,field_names,*,rename=False,defaults=None,module=None)
  84. Point = collections.namedtuple("Point",["x","y"])
  85. #对Point对域名赋值,完成初始化操作
  86. p = Point(1,y=2)
  87. print(p)
  88. #p作为一个元组,具有元组的属性,可以调用属性,解包赋值
  89. print(p.x)
  90. print(p.y)
  91. #有元组的性质
  92. print(p[0])
  93. print(p[1])
  94. x,y = p
  95. print(x)
  96. print(y)
  97. print(isinstance(p,tuple))
  98. #Counter 计数器工具
  99. from collections import Counter
  100. s = "牛奶奶找刘奶奶买牛奶"
  101. colors = ['red','blue','red','green','blue','blue']
  102. cnt_str = Counter(s)
  103. cnt_color = Counter(colors)
  104. print(cnt_str)
  105. print(cnt_color)
  106. #Counter是字典类型的子类
  107. print(isinstance(Counter(),dict))
  108. #最常见的统计 -- most_commom(n) 提供n个频率最高的元组和计数
  109. #比如打印出cnt_color中2个出现频率最高的元素和计数
  110. print(cnt_color.most_common(2))
  111. #将已经统计好的元素展开 -- elements()
  112. print(list(cnt_str.elements()))
  113. #其他一些加减操作
  114. c = Counter(a=3,b=1)
  115. d = Counter(a=1,b=2)
  116. print(c+d)
  117. #双向队列 可以方便的在队列两边高效、快速的增加和删除元素
  118. from collections import deque
  119. d = deque('cde')
  120. print(d)
  121. d.append("f") #右端增加
  122. d.append("g")
  123. d.appendleft("b")#左端增加
  124. d.appendleft("g")
  125. print(d)
  126. d.pop() #右端删除
  127. d.popleft()#左端删除
  128. print(d)
  129. print("================itertools标准库===========")
  130. #itertools标准库中有很多产生迭代器的方法
  131. #排列组合迭代器
  132. #product -- 笛卡尔积
  133. import itertools
  134. for i in itertools.product('ABC','01'):
  135. print(i)
  136. for i in itertools.product('ABC',repeat = 3):
  137. print (i)
  138. #permutations 排列
  139. for i in itertools.permutations('ABCD',3): #从ABCD中取3个进行排列,3是排列的长度
  140. print(i)
  141. for i in itertools.permutations(range(3)):
  142. print(i)
  143. #combinations 组合
  144. for i in itertools.combinations('ABCD',2): #2是组合的长度
  145. print(i)
  146. for i in itertools.combinations(range(4),3):
  147. print(i)
  148. #combinations_with_replacement -- 元素可重复组合
  149. for i in itertools.combinations_with_replacement('ABC',2): #2是组合的长度
  150. print(i)
  151. for i in itertools.product('ABC',repeat=2):
  152. print(i)
  153. #拉链
  154. #zip -- 短拉链
  155. for i in zip("ABC","012","xyz"):
  156. print(i)
  157. #长度不一致时,执行到最短的对象处,就停止
  158. for i in zip("ABC","012345"):
  159. print(i)
  160. #长度不一致时,执行到最长对象处,就停止,缺省的元素用None或指定字符替代
  161. for i in itertools.zip_longest("ABC","012345"):
  162. print(i)
  163. for i in itertools.zip_longest("ABC","012345",fillvalue="?"):
  164. print(i)
  165. #无穷迭代器
  166. #count(start=0,step=1) 计数
  167. #创建一个迭代器 它从start值开始,返回均匀间隔的值
  168. print(itertools.count(10))
  169. #cycle 循环
  170. #创建一个迭代器,返回iterable中所有元素,无限重复
  171. print(itertools.cycle("ABC"))
  172. #repeat(object[,times]) --重复
  173. #其他迭代器
  174. #chain(iterables) 锁链 把一组迭代对象串联起来 形成一个更大的迭代器
  175. #enumerate(iterable,start=0) 枚举 python内置
  176. #groupby(iterable,key=None) 分组

发表评论

表情:
评论列表 (有 0 条评论,50人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python 标准

    [Python 语言参考手册][Python] 描述了 Python 语言的具体语法和语义,这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Pyth

    相关 Python标准

    一、正则表达式 (re包) 我将从正则表达式开始讲Python的标准库。正则表达式是文字处理中常用的工具,而且不需要额外的系统知识或经验。我们会把系统相关的包放在后面讲解