PYTHON的数据结构和算法介绍

本文介绍了Python中的基本数据结构,如集合去重、字典存储键值对、数组的堆栈和队列操作,以及二叉树的概念。此外,还提到一份面向不同水平Python开发者的学习资源,提供体系化的学习路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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”:“Don’t 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’)

链表

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

blog.csdnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值