python零碎知识(7)--自带电池(标准库,模块)
来源:互联网 发布:php 获得父类的属性 编辑:程序博客网 时间:2024/05/29 03:15
1,python3.0中去掉了reload
函数,可用exec
实现同样的功能
2.在模块中加入测试代码
1)需要‘告知’模块本身是作为程序运行还是导入到其他程序,__name__
2)在主程序中,变量__name__
的值是__main__
,而在导入模块中,这个值设定为模块的名字
__name__>>>'__main__'hello2.__name__>>>'hello2'
2)综上,可将模块的测试代码放入if
语句中
def hello(): print "Hello, world!"def test(): hello()if __name__=='__main__': test()
3.探究模块
ex:导入copy
模块
1)dir
查看模块包含的内容,会将对象所有的特性(以及模块所有的函数、类、变量等)列出
用法:dir(copy)
2)__all__
变量
定义了模块的公有接口,即告诉解释器,从模块导入所有名字代表什么含义
copy.__all__>>>['Error', 'copy', 'deepcopy']
from copy import * #表示引入__all__变量中的函数
3)help
获取帮助
help(copy.copy)>>>Help on function copy in module copy:copy(x) Shallow copy operation on arbitrary Python objects. See the module's __doc__ string for more info.
4)__doc__
字符串
写在函数的开头,并且简述函数的功能
print range.__doc__>>>range(stop) -> list of integersrange(start, stop[, step]) -> list of integersReturn a list containing an arithmetic progression of integers.range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.When step is given, it specifies the increment (or decrement).For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!These are exactly the valid indices for a list of 4 elements.
4.标准库
1)sys
模块:访问与python解释器联系紧密的变量和函数
2)os
模块:提供访问多个操作系统服务 webbrowser
模块,包含open
函数,可以自动启动Web浏览器访问给定的URL
import webbrowserwebbrowser.open('http://www.baidu.com')
3)fileinput
:遍历文本文件的所有行
4)set
集合
mySet=[]for i in range(10): mySet.append(set(range(i,i+5)))reduce(set.union,mySet)>>>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
集合主要用于检查成员资格,因此不包含重复元素(副本)
集合是可变得,不能用作字典中的健
因集合本身只能包含不可变(可散列,hashable)的值,所以也就不能包含其他集合,可用frozenset
表示不可变得集合
a=set()b=set()a.add(b) >>>Traceback (most recent call last): File "<ipython-input-6-e83a439779ab>", line 1, in <module> a.add(b)TypeError: unhashable type: 'set'a.add(frozenset(b))
5)heapq
堆与队列
heap中元素的顺序很重要,如下
from heapq import *from random import shuffledata=range(10)heap=[]for n in data: heappush(heap,n)heap>>>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]heappush(heap,0.5)heap>>>[0, 0.5, 2, 3, 1, 5, 6, 7, 8, 9, 4]
堆属性:i位置处的元素总比2*i以及2*i+1位置处的元素小
heappop
弹出最小的元素,一般来说都是在索引0处的元素,并且会确保剩余元素中最小的那个占据这个位置,保持堆属性
heappop(heap)Out[19]: 0heappop(heap)Out[20]: 0.5heappop(heap)Out[21]: 1
heapify
:使用任意列表作为参数,并且通过尽可能少的移位操作,将其转换为合法的堆 heapreplace
:弹出堆的最小元素,并且将新元素推入 nlargest
:寻找任何可迭代对象iter中第n大的元素nlargest(n,iter)
nsmallest
:寻找任何可迭代对象iter中第n小的元素nsmallest(n,iter)
6)deque
双端队列,doubled-ended queue:按照元素增加的顺序来移除元素
from collections import deque #deque位于collentions模块中q=deque(range(5))q.append(5)q.appendleft(6)qOut[22]: deque([6, 0, 1, 2, 3, 4, 5])q.pop()Out[23]: 5q.popleft()Out[25]: 6
7)time
:获取当前时间,操作时间和日期,从字符串读取时间以及格式化时间为字符串
8)random
返回包含随机数的函数,可以用于模拟或者用于任何产生随机输出的程序
9)shelve
简单的存储 open,close
10)re
:包含对正则表达式的支持
正则表达式:可以匹配文本片段的模式,最简单的正则表达式是普通字符串,可以匹配其本身 .
点号:通配符,可以匹配任何字符串(除换行符外的任何单个字符) re
中利用\\
对特殊字符进行转义,解释器通过\
字符集:扩宽匹配范围
11)模板系统
字符串格式化[something]
- python零碎知识(7)--自带电池(标准库,模块)
- 第十章 自带电池(模块)
- python零碎知识(3)--字典
- python零碎知识(4)--语句
- python零碎知识(5)--对象
- 第十章-自带电池——python基础教程(第二版)笔记
- [python]python标准库(模块)简介
- Python自带日志模块
- python零碎知识(6)--魔法方法、属性和迭代器
- Python零碎知识(8):模块的学习|资源利用
- Python零碎知识(7):硬性出错
- Python 零碎知识
- python 自带urllib.request 模块简单介绍(python3.5)
- python自带模块发送邮件
- Python自带模块及其作用
- python 不可与自带模块重名
- Python零碎知识之zip
- Python资料之零碎知识
- C++中的多继承
- mysql Error2003的错误
- Ubuntu16.04+GTX1070显卡驱动+cuda8.0+cudnn5.1+tensorflow-gpu环境配置
- 下拉多选搜索控件
- codeBlocks安装小注意
- python零碎知识(7)--自带电池(标准库,模块)
- luogu3374 树状数组
- 细说 new与 malloc 的 10 点区别
- 2017icpc 北京网络赛 A Visiting Peking University
- js实现购物车加减和价格运算
- keras评估模型
- [洛谷]P1083 借教室
- mac下Yii2如何安装advanced,mac下yii2 advanced安装教程
- C#之工大租车系统