您现在的位置是:亿华云 > IT科技类资讯

PYTHON的数据结构和算法介绍

亿华云2025-10-02 18:47:28【IT科技类资讯】6人已围观

简介当你听到数据结构时,你会想到什么?数据结构是根据类型组织和分组数据的容器。它们基于可变性和顺序而不同。可变性是指创建后改变对象的能力。我们有两种类型的数据结构,内置数据结构和用户定义的数据结构。什么是

当你听到数据结构时,据结介绍你会想到什么?构和

数据结构是根据类型组织和分组数据的容器。它们基于可变性和顺序而不同。算法可变性是据结介绍指创建后改变对象的能力。我们有两种类型的构和数据结构,内置数据结构和用户定义的算法数据结构。

什么是据结介绍数据算法-是由计算机执行的一系列步骤,接受输入并将其转换为目标输出。构和

内置数据结构

列表

列表是算法用方括号定义的,包含用逗号分隔的据结介绍数据。该列表是构和可变的和有序的。它可以包含不同数据类型的算法混合。

months=[january,据结介绍february,march,april,may,june,july,august,september,october,november,december]

print(months[0])#print the element with index 0

print(months[0:7])#all the elements from index 0 to 6

months[0]=birthday #exchange the value in index 0 with the word birthday

print(months)

元组

元组是另一种容器。它是构和不可变有序元素序列的数据类型。不可变的亿华云算法,因为你不能从元组中添加和删除元素,或者就地排序。

length, width, height =9,3,1 #We can assign multiple variables in one

shot

print("The dimensions are { } * { } * { }".format(length, width, height))一组

集合是唯一元素的可变且无序的集合。它可以让我们快速地从列表中删除重复项。

numbers=[1,2,3,4,6,3,3]

unique_nums = set(numbers)

print(unique_nums)

models ={ declan,gift,jabali,viola,kinya,nick,betty }

print(davis in models)#check if there is turner in the set models

models.add(davis)

print(model.pop())remove the last item#字典

字典是可变和无序的数据结构。它允许存储一对项目(即键和值)

下面的例子显示了将容器包含到其他容器中来创建复合数据结构的可能性。

music={ jazz:{ "coltrane": "In a sentiment mood",

"M.Davis":Blue in Green",

"T.Monk":"Dont Blame Me"},

"classical":{ "Bach": "cello suit",

"Mozart": "lacrimosa",

"satle": "Gymnopedie"}}

print(music["jazz"]["coltrane])#we select the value of the key coltrane

print(music["classical"] [mozart"])

使用数组的堆栈堆栈是一种线性数据结构,其中元素按顺序排列。它遵循L.I.F.O的机制,意思是后进先出。因此,最后插入的元素将作为第一个元素被删除。这些操作是:

将元素推入堆栈。从堆栈中删除一个元素。

要检查的条件

溢出情况——当我们试图在一个已经有最大元素的堆栈中再放一个元素时,就会出现这种情况。下溢情况——当

我们试图从一个空堆栈中删除一个元素时,香港云服务器就会出现这种情况。class mystack:

def __init__(self):

self.data =[]

def length(self): #length of the list

return len(self.data)

def is_full(self): #check if the list is full or not

if len(self.data) == 5:

return True

else:

return False

def push(self, element):# insert a new element

if len(self.data) < 5:

self.data.append(element)

else:

return "overflow"

def pop(self): # # remove the last element from a list

if len(self.data) == 0:

return "underflow"

else:

return self.data.pop()

a = mystack() # I create my object

a.push(10) # insert the element

a.push(23)

a.push(25)

a.push(27)

a.push(11)

print(a.length())

print(a.is_full())

print(a.data)

print(a.push(31)) # we try to insert one more element in the list - the

output will be overflow

print(a.pop())

print(a.pop())

print(a.pop())

print(a.pop())

print(a.pop())

print(a.pop()) # try to delete an element in a list without elements - the

output will be underflow

使用数组排队

队列是一种线性数据结构,其中的元素按顺序排列。它遵循先进先出的F.I.F.O机制。

描述队列特征的方面

两端:

前端-指向起始元素。指向最后一个元素。

有两种操作:

enqueue——将元素插入队列。它将在后方完成。出列-从队列中删除元素。这将在前线完成。

有两个条件。

溢出-插入到一个已满的队列中。下溢-从空队列中删除。class myqueue:

def __init__(self):

self.data = []

def length(self):

return len(self.data)

def enque(self, element): # put the element in the queue

if len(self.data) < 5:

return self.data.append(element)

else:

return "overflow"

def deque(self): # remove the first element that we have put in queue

if len(self.data) == 0:

return "underflow"

else:

self.data.pop(0)

b = myqueue()

b.enque(2) # put the element into the queue

b.enque(3)

b.enque(4)

b.enque(5)

print(b.data)

b.deque()# # remove the first element that we have put in the queue

print(b.data)

树(普通树)

树用于定义层次结构。它从根节点开始,再往下,最后的节点称为子节点。

在本文中,我主要关注二叉树。二叉树是一种树形数据结构,其中每个节点最多有两个孩子,称为左孩子和右孩子。

# create the class Node and the attrbutes

class Node:

def __init__(self, letter):

self.childleft = None

self.childright = None

self.nodedata = letter

# create the nodes for the tree

root = Node(A)

root.childleft = Node(B)

root.childright = Node(C)

root.childleft.childleft = Node(D)

root.childleft.childright = Node(E)链表

它是具有一系列连接节点的线性数据。每个节点存储数据并显示到下一个节点的云服务器路由。它们用来实现撤销功能和动态内存分配。

class LinkedList:

def __init__(self):

self.head = None

def __iter__(self):

node = self.head

while node is not None:

yield node

node = node.next

def __repr__(self):

nodes = []

for node in self:

nodes.append(node.val)

return " -> ".join(nodes)

def add_to_tail(self, node):

if self.head == None:

self.head = node

return

for current_node in self:

pass

current_node.set_next(node)

def remove_from_head(self):

if self.head == None:

return None

temp = self.head

self.head = self.head.next

return temp

class Node:

def __init__(self, val):

self.val = val

self.next = None

def set_next(self, node):

self.next = node

def __repr__(self):

return self.val图表这是一种数据结构,它收集了具有连接到其他节点的数据的节点。

它包括:

顶点的集合。边E的集合,表示为有序的顶点对(u,v)算法

在算法方面,我不会讲得太深,只是陈述方法和类型:

分而治之——以将问题分成子部分并分别解决而闻名。动态-它将问题分成子部分,记住子部分的结果,并将其应用于类似的问题。贪婪算法——在解决问题时采取最简单的步骤,而不用担心未来的复杂性。

算法的类型

树遍历算法是非线性数据结构。以根和节点为特征。这里有三种类型的按序遍历,前序遍历和后序遍历。排序算法——用于按照给定的顺序对数据进行排序。可分为归并排序和冒泡排序。搜索算法——用于寻找给定数据集中存在的一些元素。一些类型的搜索算法是:线性搜索,二分搜索法和指数搜索。

很赞哦!(2)