1.常用标准库
1.1 sys模块
import sys
for i in range(100):
print(i)
if i ==5:
sys.exit(0)
print('\n\nPython 路径为:', sys.path, '\n')
print(sys.platform)
print(sys.version)
1.2 os模块
import os
print("当前进程的ID: ",os.getpid())
print("当前父进程的ID: ", os.getppid())
cwd = os.getcwd()
print("当前所在路径为: ",cwd)
os.chdir("c:\\")
print("修改后当前所在路径为: ", os.getcwd())
print("当前目录下的文件有: ", os.listdir(cwd))
for root, dirs, files in os.walk(cwd, topdown=False):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
import os
print("text.txt 的绝对路径为: ",os.path.abspath("text.txt"))
print("text.txt是否存在: ",os.path.exists("text.txt"))
print("text.txt的文件大小: ",os.path.getsize("text.txt"))
print("text.txt是否为文件:",os.path.isfile("text.txt"))
print("text.txt是否为文件夹:",os.path.isdir("text.txt"))
1.3 time模块
import time
time_now = time.time()
print("时间戳:",time_now)
localtime = time.localtime(time_now)
print("本地时间为:" , localtime)
localtime = time.asctime(localtime)
print("本地时间为:", localtime)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
2.数据结构
2.1 单链表
class Link_Node():
def __init__(self, data, next_link=None):
self.data = data
self.next_link = next_link
class Link_Manipulate():
def __init__(self):
self._head = None
def isEmpty(self):
return self._head is None
def length(self):
node = self._head
count = 0
while node:
count += 1
node = node.next_link
return count
def add(self, data):
node = Link_Node(data)
node.next_link = self._head
self._head = node
def travel(self):
if self._head:
cur_node = self._head
while cur_node:
print(cur_node.data, end=" ")
cur_node = cur_node.next_link
else:
raise ("The Link is empty!")
def append(self, data):
node = Link_Node(data)
if self._head is None:
self._head = node
return
else:
cur_node = self._head
while cur_node:
pre_node, cur_node = cur_node, cur_node.next_link
pre_node.next_link = node
def insert(self, data, index):
if index <= 0:
self.add(data)
elif index >= self.length():
self.append(data)
else:
node = Link_Node(data)
cur_node = self._head.next_link
pre_node = self._head
count = 1
while cur_node:
if count == index:
pre.next_link, node.next_link = node, cur_node
break
pre, cur_node = cur_node, cur_node.next_link
count += 1
def remove(self, data):
if self.isEmpty():
return "Failed because of Empty!"
cur_node = self._head
pre_node = None
while cur_node:
if cur_node.data == data:
if pre_node == None:
self._head = cur_node.next_link
else:
pre_node.next_link = cur_node.next_link
return data
pre_node, cur_node = cur_node, cur_node.next_link
raise ("Not found!")
def search(self, data):
if self.isEmpty():
raise ("The Link is empty!")
else:
cur_node = self._head
index = 0
while cur_node:
if cur_node.data == data:
return index
index += 1
cur_node = cur_node.next_link
return -1
Link_Demo = Link_Manipulate()
Link_Demo.add(1)
Link_Demo.append(2)
Link_Demo.append(3)
Link_Demo.append(4)
Link_Demo.append(5)
print("链表节点是否为空:", str(Link_Demo.isEmpty()))
print("链表节点长度为:", str(Link_Demo.length()))
print("遍历链表:")
Link_Demo.travel()
Link_Demo.insert(6, 2)
print("遍历链表:")
Link_Demo.travel()
Link_Demo.remove(6)
print("遍历链表:")
Link_Demo.travel()
2.2 双链表
class Double_link_Node():
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class Double_Link(object):
def __init__(self):
self._head = None
def isEmpty(self):
return self._head == None
def length(self):
cur_data = self._head
count = 0
while cur_data != None:
count=count+1
cur_data = cur_data.next
return count
def travel(self):
cur_data = self._head
while cur_data != None:
print(cur_data.data,end=" ")
cur_data = cur_data.next
print("travel end!!!")
def add(self, data):
Dnode = Double_link_Node(data)
if self.isEmpty():
self._head = Dnode
else:
Dnode.next = self._head
self._head.prev = Dnode
self._head = Dnode
def append(self, data):
Dnode = Double_link_Node(data)
if self.isEmpty():
self._head = Dnode
else:
cur_data = self._head
while cur_data.next != None:
cur_data = cur_data.next
cur_data.next = Dnode
Dnode.prev = cur_data
def search(self, data):
cur_data = self._head
while cur_data != None:
if cur_data.data== data:
return True
cur_data = cur_data.next
return False
def insert(self, data, index):
if index <= 0:
self.add(data)
elif index > (self.length()-1):
self.append(data)
else:
Dnode = Double_link_Node(data)
cur_data = self._head
count = 0
while count < (index-1):
count += 1
cur_data = cur_data.next
Dnode.prev = cur_data
Dnode.next = cur_data.next
cur_data.next.prev = Dnode
cur_data.next = Dnode
def remove(self, data):
if self.isEmpty():
return
else:
cur_data = self._head
if cur_data.data == data:
if cur_data.next == None:
self._head = None
else:
cur_data.next.prev = None
self._head= cur_data.next
return
while cur_data != None:
if cur_data.data == data:
cur_data.prev.next = cur_data.next
cur_data.next.prev = cur_data.prev
break
cur_data = cur_data.next
Double_Link_Demo = Double_Link()
Double_Link_Demo.add(1)
Double_Link_Demo.append(2)
Double_Link_Demo.append(3)
Double_Link_Demo.append(4)
Double_Link_Demo.append(5)
print("链表节点是否为空:",str(Double_Link_Demo.isEmpty()))
print("链表节点长度为:",str(Double_Link_Demo.length()))
print("遍历链表:")
Double_Link_Demo.travel()
Double_Link_Demo.insert(6,2)
print("遍历链表:")
Double_Link_Demo.travel()
Double_Link_Demo.remove(6)
print("遍历链表:")
Double_Link_Demo.travel()
2.3 二叉树
class BinaryTreeNode(object):
def __init__(self):
self.data = '#'
self.leftChild = None
self.rightChild = None
class BinaryTree(object):
def createBinaryTree(self, Root):
data = input('==>')
if data == '#':
Root = None
else:
Root.data = data
Root.leftChild = BinaryTreeNode()
self.createBinaryTree(Root.leftChild)
Root.rightChild = BinaryTreeNode()
self.createBinaryTree(Root.rightChild)
def preOrder(self, Root):
if Root is not None:
self.visitBinaryTreeNode(Root)
self.preOrder(Root.leftChild)
self.preOrder(Root.rightChild)
def inOrder(self, Root):
if Root is not None:
self.inOrder(Root.leftChild)
self.visitBinaryTreeNode(Root)
self.inOrder(Root.rightChild)
def postOrder(self, Root):
if Root is not None:
self.postOrder(Root.leftChild)
self.postOrder(Root.rightChild)
self.visitBinaryTreeNode(Root)
def visitBinaryTreeNode(self, BinaryTreeNode):
if BinaryTreeNode.data is not '#':
print(BinaryTreeNode.data,end="->")
if __name__== '__main__':
bTN = BinaryTreeNode()
bT= BinaryTree()
bT.createBinaryTree(bTN)
print('先序遍历的输出结果为:')
bT.preOrder(bTN)
print('\n中序遍历的输出结果为:')
bT.inOrder(bTN)
print('\n后序遍历的输出结果为:')
bT.postOrder(bTN)
|