[关闭]
@WillireamAngel 2018-05-27T13:54:50.000000Z 字数 5263 阅读 2031

Python数据结构

数据结构


线性数据结构

Array

  1. from array import *
  2. arrayName = array(typecode, [Initializers])

array

ASCII码:英文字母一个字节,中文两个字节,八位二进制数
UTF-8:英文字符(含标点)一个字节,中文字符(含标点)三个字节。
Unicode:中英文及其标点均是两个字符。

  1. for i in array:
  2. print(i)

插入

  1. array.insert(index_num,content)

删除

  1. array.remove(content)

搜索

  1. array.index(content)

更新

  1. array[index_num]=content

二维数组

数组嵌套实现。
[array1,array2.....]

  1. for i in T:
  2. for j in i:
  3. print(c,end = " ")
  4. print()

Node

节点,类似于指针的作用,数据和数据元素的下一个位置的地址也会被存储,内存分配到非连续块中,是链表和树的基础。
创建实现一个保存指针和数据元素的类来实现的。
创建和遍历:

  1. #定义和初始化类
  2. class mint:
  3. def __init__(self,deta=None):
  4. self.data = data
  5. self.next = None
  6. #创建数据
  7. e1 = mint('Mon')
  8. e2 = mint('Tue')
  9. e3 = mint('wed')
  10. #创建指针
  11. e1.next = e3
  12. e3.next = e2
  13. #遍历元素
  14. this = e1
  15. while this:
  16. print(this.data)
  17. this = this.next

Linked List

单链表

  1. class Node:
  2. def __init__(self, dataval=None):
  3. self.dataval = dataval
  4. self.nextval = None
  5. class SLinkedList:
  6. def __init__(self):
  7. self.headval = None
  8. list1 = SLinkedList()
  9. list1.headval = Node("Mon")
  10. e2 = Node("Tue")
  11. e3 = Node("Wed")
  12. # Link first Node to second node
  13. list1.headval.nextval = e2
  14. # Link second Node to third node
  15. e2.nextval = e3

或者

  1. class Node:
  2. def __init__(self, data=None):
  3. self.data = data
  4. self.next = None
  5. class SLinkedList:
  6. def __init__(self):
  7. self.head = None
  8. def Atbegining(self, data_in):
  9. NewNode = Node(data_in)
  10. NewNode.next = self.head
  11. self.head = NewNode
  12. llist = SLinkedList()
  13. llist.Atbegining("Mon")
  14. llist.Atbegining("Tue")
  15. llist.Atbegining("Wed")
  16. llist.Atbegining("Thu")
  17. llist.LListprint()

遍历:

  1. def LListprint(self):
  2. printval = self.head
  3. while (printval):
  4. print(printval.data),
  5. printval = printval.next

插入:

  1. def AtBegining(self,newdata):
  2. NewNode = Node(newdata)
  3. # Update the new nodes next val to existing node
  4. NewNode.nextval = self.headval
  5. self.headval = NewNode
  1. def AtEnd(self, newdata):
  2. NewNode = Node(newdata)
  3. if self.headval is None:
  4. self.headval = NewNode
  5. return
  6. laste = self.headval
  7. while(laste.nextval):
  8. laste = laste.nextval
  9. laste.nextval=NewNode
  1. def Inbetween(self,middle_node,newdata):
  2. if middle_node is None:
  3. print("The mentioned node is absent")
  4. return
  5. NewNode = Node(newdata)
  6. NewNode.nextval = middle_node.nextval
  7. middle_node.nextval = NewNode

删除:

  1. def RemoveNode(self, Removekey):
  2. HeadVal = self.head
  3. if (HeadVal is not None):
  4. if (HeadVal.data == Removekey):
  5. self.head = HeadVal.next
  6. HeadVal = None
  7. return
  8. while (HeadVal is not None):
  9. if HeadVal.data == Removekey:
  10. break
  11. prev = HeadVal
  12. HeadVal = HeadVal.next
  13. if (HeadVal == None):
  14. return
  15. prev.next = HeadVal.next
  16. HeadVal = None

双链表

双链表:第一个和最后一个的链接元素,每个链表有一个数据字段和两个被称为next和prev的链接字段。每个链接可与上一个链接和下一个链接链接,最后一个链表使用空来标记列表的结尾。
在单链表的基础上添加prev标记
插入、附加

Stack

堆栈,后进先出
添加和删除元素的操作称为PUSH和POP操作

  1. class Stack:
  2. def __init__(self):
  3. self.stack = []
  4. def add(self, dataval):
  5. if dataval not in self.stack:
  6. self.stack.append(dataval)
  7. return True
  8. else:
  9. return False
  10. def peek(self):
  11. return self.stack[-1]
  12. def remove(self):
  13. if len(self.stack) <= 0:
  14. return ("No element in the Stack")
  15. else:
  16. return self.stack.pop()

Queue

队列

先进先出
insert()添加pop()删除移除元素

双端队列

双端队列可从任一端添加和删除元素
collections模块

  1. import collections

创建双端队列:collections.deque(list)
append()
appendleft()
clear() copy()
count() extend() extendleft()
insert() pop() popleft()
remove() reverse()

Matrix

使用numply进行矩阵数据管理。

  1. from numpy import *
  2. a =array([['wen',1,2,3,1],['hai',4,5,6,4],['ping',7,8,9,7]]) #多中括号

非线性数据结构

Binary Tree

二叉树
树结构:仅有一个父节点
定义左节点和右节点,插入树中

二叉搜索树:
BST:Binary Search Tree
节点的左子树小于等于其父节点的值,节点的右子树大于其父节点的值

  1. left_subtree (keys) node (key) right_subtree (keys)

搜索的基本过程是按大小左右搜索

Heap

堆:
每个父节点小于或等于其子节点,最小堆;每个父节点大于或等于子节点,最大堆。
创建堆:
heapq库:heapify常规列表转换为堆,最小元素索引0;heappush堆在最后的索引处添加一个元素而不改变当前堆;heappop返回堆中最小的数据元素,索引为0;heapreplace用新值替换最小的数据元素,在未被修复的地方插入新值。

Hash Table

散列表
数据元素的地址或索引值是由散列函数生成的,哈希表存储键值对,密钥通过哈希函数生成。
通过字典类型实现哈希表(密钥):
字典键可散列,具备唯一值生成结果,字典数据元素顺序不固定。

Graph

一组对象通过链接连接的一组对象的图形表示。互连对象为顶点,连接顶点的链接为边。
基本操作:
显示图形顶点:直接获取图形字典的关键字
显示图形边缘:遍历顶点关系,获取图形关系集合列表
添加顶点:新定义一个键
添加边缘:添加顶点
创建图

Python特征数据结构

list

tuple

Dictionary

Set

set([])创建集合
访问集合值=循环打印
添加:add()
删除:discard()
集合运算:并| 交& 差- 比较<=>

Reference

URL

https://www.tutorialspoint.com/python/python_data_structure_introduction.htm

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注