python之栈和队列
1. 栈
1.1 示例
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: .py
@time: 2018-07-20 9:19
@desc: 先入后出
'''
queue = [] #入栈
print('************入栈*************')
queue.append('A')
print(queue)
queue.append('B')
print(queue)
queue.append('C')
print(queue) #出栈
print('************出栈*************')
queue.pop()
print(queue)
queue.pop()
print(queue)
queue.pop()
print(queue)
1.2 运行结果

2. 队列
2.1 示例
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 队列.py
@time: 2018-07-20 9:27
@desc: 先入先出
'''
import collections queue =collections.deque() #入栈
print('************入栈*************')
queue.append('A')
print(queue)
queue.append('B')
print(queue)
queue.append('C')
print(queue) #出栈
print('************出栈*************')
queue.popleft()
print(queue)
queue.popleft()
print(queue)
queue.popleft()
print(queue)
2.2 运行结果

3. 案例
3.1 递归遍历目录
3.1.1 概念
递归就是一个函数在它的函数体内调用它自身。执行递归函数将反复调用其自身,每调用一次就进入新的一层。递归函数必须有结束条件。
当函数在一直递推,直到遇到墙后返回,这个墙就是结束条件。
所以递归要有两个要素,结束条件与递推关系
3.1.2 示例
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 递归遍历目录.py
@time: 2018-07-20 9:31
@desc:
'''
import os def getAllDir(path,sp=''):
#获取目录
filesList=os.listdir(path)
for fileName in filesList:
#文件的绝对路径
fileAbsPath = os.path.join(path,fileName)
#判断是否为文件
if os.path.isfile(fileAbsPath):
print(sp+ '普通文件:',fileAbsPath) else:
print(sp+ '目录:', fileAbsPath)
sp += ' '
getAllDir(fileAbsPath,sp) getAllDir(r'D:\testproject\core')
3.1.3 运行结果

3.2栈方式递归目录
3.2.1 示例
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 栈方式遍历目录.py
@time: 2018-07-20 10:16
@desc:
'''
import os def getAllDir(path,sp=''):
stack=[]
stack.append(path) #当栈为空时,目录遍历完
while len(stack) !=0:
#从栈取数据
fileDir=stack.pop()
filesList = os.listdir(fileDir) for fileName in filesList:
# 文件的绝对路径
fileAbsPath = os.path.join(fileDir, fileName)
# 判断是否为文件
if os.path.isfile(fileAbsPath):
print('普通文件:', fileName) else:
print('目录:', fileName)
stack.append(fileAbsPath) getAllDir(r'D:\testproject\core')
3.2.2 运行结果

3.3 队列方式递归目录
3.3.1 示例
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 队列方式遍历目录.py
@time: 2018-07-20 14:41
@desc:
'''
import os
import collections def getAllDir(path,sp=''):
queue =collections.deque()
queue.append(path) #当栈为空时,目录遍历完
while len(queue) !=0:
#从栈取数据
fileDir=queue.popleft()
filesList = os.listdir(fileDir) for fileName in filesList:
# 文件的绝对路径
fileAbsPath = os.path.join(fileDir, fileName)
# 判断是否为文件
if os.path.isfile(fileAbsPath):
print('普通文件:', fileName) else:
print('目录:', fileName)
queue.append(fileAbsPath) getAllDir(r'D:\testproject\core')
3.3.2 运行结果

python之栈和队列的更多相关文章
- 【DataStructure In Python】Python模拟栈和队列
用Python模拟栈和队列主要是利用List,当然也可以使用collection的deque.以下内容为栈: #! /usr/bin/env python # DataStructure Stack ...
- 使用python实现栈和队列
1.使用python实现栈: class stack(): def __init__(self): self.stack = [] def empty(self): return self.stack ...
- Python实现栈、队列
目录 1. 栈的Python实现 1.1 以列表的形式简单实现栈 1.2 以单链表形式实现栈 2. 队列的Python实现 2.1 以列表实现简单队列 2.2 以单链表形式实现队列 本文将使用py ...
- python之栈与队列
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- Python 实现栈与队列
#基于Python2.7 #基于顺序表实现 #发现用Python写题时,没有像写C++时方便的STL可用,不过查阅资料之后发现用class实现也很简洁,不过效率应该不是很高 Python实现栈并使用: ...
- python之 栈与队列
忍不住想报一句粗口"卧槽"这尼玛python的数据结构也太特么方便了吧 想到当初学c语言的数据结构的时候,真的是一笔一划都要自己写出来,这python尼玛直接一个模块就ok 真的是 ...
- Python数据结构——栈、队列的实现(二)
1. 一个列表实现两个栈 class Twostacks(object): def __init__(self): self.stack=[] self.a_size=0 self.b_size=0 ...
- Python数据结构——栈、队列的实现(一)
1. 栈 栈(Stack)是限制插入和删除操作只能在一个位置进行的表,该位置是表的末端,称为栈的顶(top).栈的基本操作有PUSH(入栈)和POP(出栈).栈又被称为LIFO(后入先出)表. 1.1 ...
- Python的栈和队列实现
栈 class Node: def __init__(self, data=None): self.next = None self.data = data class Stack: def __in ...
随机推荐
- Ajax 要点
Ajax 全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML) Ajax的实现是基于 xmlHttp对象 异步发送请求 XMLHttpReque ...
- linux 显示系统执行的进程
ps -a 显示所有的进程信息 -u 以用户的形式显示系统进程 -x 显示后台进程运行的参数 netstat -anp |more 查看端口 查看开放的端口 vim/etc/sysconfig/ ...
- java的第一次博客
一枚16年本科毕业的java程序员,至今工作两年,这是我的第一个博客. 谢谢!!!
- PHP中汉字截取
$len = 19; $text = "怎么将新闻的很长的标题只显示前面一些字,后面用.....来代替?"; echo strlen($text)<=$len ? $text ...
- 深度学习原理与框架-卷积神经网络基本原理 1.卷积层的前向传播 2.卷积参数共享 3. 卷积后的维度计算 4. max池化操作 5.卷积流程图 6.卷积层的反向传播 7.池化层的反向传播
卷积神经网络的应用:卷积神经网络使用卷积提取图像的特征来进行图像的分类和识别 分类 相似图像搜索 ...
- day13-文件操作
1.打开与关闭 1.1.open() close()我们使用 open() 函数打开文件.这个函数将返回一个文件对象,我们对文件的读写都将使用这个对象.open() 函数需要三个参数,第一个参数是文件 ...
- jquery 属性-记住
$("").attr(); $("").removeAttr(); $("").prop(); $("").remove ...
- struts2默认临时文件更改
struts的文件上传mutifile会有一个临时文件地址,如果需要使用自己指定临时文件地址需要在struts.xml中设置以下内容. <constant name="struts.m ...
- Mongo 应用查询
官网操作手册,基本就够用 https://docs.mongodb.com/manual/ 下面是个分组查询的例子,项目中用到然后查了个例子,自己理解了下,觉得很好很强大. https://blog. ...
- 深入Spring Boot:怎样排查expected single matching bean but found 2的异常
写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常. https://github.com/hengy ...