# -*- coding: cp936 -*-
#set集合
a = set([1,2,3])
b = set([2,3,4])
print(a|b)#a或b
print(a&b)#a且b
print(a-b)#a去除b
print(a.difference(b))#等同上一个方法
print(a^b)#去除ab相同

print(a|b>b)
print(a&b<b)

#heap
#堆栈原理
#i位置上元素总比i//2位置的元素大
#即2i和2i+1的元素总比i的元素大
from heapq import *
from random import shuffle
data = range(10)
shuffle(data)
heap = []
for n in data:
    heappush(heap,n)
print(heap)
heappush(heap,0.5)
print(heap)
print(heappop(heap))#弹出最小的元素
print(heap)
print(heapreplace(heap,0.6))#弹出最小的元素,加入0.6
print(heap)
a = [1,3,2,4,11,0,3,4,5]
heapify(a)#利用尽可能少的移位,将a转为为堆栈
print(a)

#deque双端队列

#time
import time
a=(2008,1,1,1,1,1,1,1,1)#必须为9位
print(time.asctime(a))#将元组转为为时间
print(time.asctime())#当前时间

#shelve 简单的存储
import shelve
s = shelve.open(r'C:\python.dat')#python.dat成为了数据库文件
s["x"] = ['a','b','c']
s["y"] = ['d','e','f']
s.close()
#再次打开还可以查看到上面的信息
s = shelve.open(r'C:\python.dat')
print(s["x"])
print(s["y"])
s.close()

#re函数,正则表达式
import re
pat1 = "[1-9]{2,5}"
string1 = "12345"
if re.search(pat1,string1):
    print("Found it!")
string2 = "abc.def.ghf..hhs...dsdas"
print(re.split("[.]+",string2))#打印...分隔后的序列
pat3 = "[ab]+"
string3 = "abc.def.ghf..hhs.ababab..dsdab"
print(re.findall(pat3,string3))#返回所有匹配上的内容
#sub函数可以替换

#re匹配组方法
pat3 = "([ab]+)(.*)"
string3 = "abc.def.ghf..hhs.ababab..dsdab"
m = re.match(pat3,string3)#返回所有匹配上的内容
print(m.group(1))
print(m.group(2))
print(m.start(1))#返回第一组开始位置
print(m.end(1))#返回第一组结束位置
print(m.span(1))#返回第一组开始到结束位置

#csv模块可以操作csv文件
#datetime能够满足更多time需求
#logging日志模块
#cmd模块,命令行解释器

 

运行结果:

set([1, 2, 3, 4])
set([2, 3])
set([1])
set([1])
set([1, 4])
True
True
[0, 1, 4, 2, 3, 5, 8, 9, 7, 6]
[0, 0.5, 4, 2, 1, 5, 8, 9, 7, 6, 3]
0
[0.5, 1, 4, 2, 3, 5, 8, 9, 7, 6]
0.5
[0.6, 1, 4, 2, 3, 5, 8, 9, 7, 6]
[0, 3, 1, 4, 11, 2, 3, 4, 5]
Tue Jan 01 01:01:01 2008
Tue Aug 02 14:30:11 2011
['a', 'b', 'c']
['d', 'e', 'f']
Found it!
['abc', 'def', 'ghf', 'hhs', 'dsdas']
['ab', 'ababab', 'ab']
ab
c.def.ghf..hhs.ababab..dsdab
0
2
(0, 2)

没有登录不能评论