您的位置:首页 > 编程语言 > Python开发

python13day

2019-03-13 15:26 337 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_38501057/article/details/88426907

#############
import sys, pprint

sys.path.append(‘D:/pycharm_work’)
import day_7

dir(day_7)
import hell4

hell4.hello()

pprint.pprint(sys.path)#查找模块位置

import copy

print(dir(copy))#dir查看模块内容

print([n for n in dir(copy) if not n.startswith(’_’)])

print(copy.all)#查看所有内容,模块内容

help(copy.copy)

print(copy.doc)

help(copy)

print(copy.file)

sys模块

sys.stdin # 标准输入
sys.stdout # 标准输出
sys.stderr # 标准错误

sys.exit()#退出当前程序

os模块

import os

os.linesep # 用于文本文件的字符串分割
os.system # 用于执行外部程序

fileinput模块

import fileinput

fileinput.filename # 返回当前的文件名称
fileinput.lineno # 返回当前累计行数
fileinput.filelineno # 返回当前文件行数
fileinput.isfirstline # 检查当前文件是否文件第一行
fileinput.isstdin # 检查最后一行是否来自sys.stdin
fileinput.nextfile # 关闭当前文件关闭到文件下一个
fileinput.close # 关闭序列

集合,集合元素是随意的

a = set(range(10))
print(a)
b = set([1, 2, 3])
c = set([2, 3, 4])
print(b.union(©))
print(b | c) # 找出集合的并集
print(b & c) # 找出交集
print(c.difference(b))

堆,能够以任意顺序增加对象

import heapq

heapq.heappush # 将x入堆
heapq.heappop # 将堆中最小的元素弹出
heapq.heapreplace # 将堆中最小元素弹出,并将x入堆
heapq.heapify # 将heap属性强制应用到每个列表
heapq.nlargest # 返回第n大元素
heapq.nsmallest # 返回第n小的元素

双端队列,按元素增加的顺序,移除元素非常有用

from collections import deque

q = deque(range(5))
q.append(5)
q.appendleft(6)
q.pop()
q.popleft()
q.rotate(3) # rotate旋转元素
q.rotate(-1)
print(q)

time

import time

time.asctime # 将时间元组转换为字符串
time.localtime # 将秒数转换为日期元组,以本地时间为准
time.mktime # 将时间元组转换为本地时间
time.sleep # 休眠
time.strptime # 将字符串解析为时间元组
x = time.time() # 当前世界,从新纪元到现在的秒数
t = time.strftime(’%Y-%m-%d %H:%M:%S %j %U’)
print(t)
print(time.asctime(time.localtime(x)))

random

import random

print(random.random()) # 返回0-1的伪随机数
print(random.getrandbits(3)) # 以长整形式返回n个随机位
print(random.uniform(1, 10)) # 返回a-b之间的随机数
print(random.randrange(0, 10, 2))
random.choice # 从序列中随机返回任意元素
random.shuffle # 将给定的序列进行随机移位
random.choice # 从给定序列中,选择给定数目的元素
values = list(range(1, 11)) + list(‘Jack Queen King’.split())
suits = ‘dimaonds clubs hearts spades’.split()
deck = [’%s of %s’ % (v, s) for v in values for s in suits]
random.shuffle(deck)
print(deck[:15])

shelve

import shelve

s = shelve.open(‘test_3.bat’)
s[‘x’] = [‘n’, ‘m’, ‘h’]
print(s[‘x’])

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: