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 ...
随机推荐
- 将tgz文件解压到指定目录
转:http://blog.csdn.net/zhenwenxian/article/details/4400404 tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用 ...
- PHP中Notice: unserialize(): Error at offset of bytes in on line 的解决方法
使用unserialize函数将数据储存到数据库的时候遇到了这个报错,后来发现是将gb2312转换成utf-8格式之后,每个中文的字节数从2个增加到3个之后导致了反序列化的时候判断字符长度出现了问题, ...
- day30-模块和包
一.模块介绍 1.什么是模块 在python中,一个函数封装一个功能,当一个文件中包含很多个函数,而我们在其他程序中经常会用到这个文件中的功能时,那么我们就可以将这个包含多个函数的文件封装成一个模块, ...
- mfc cef<转>
在mfc单文档程序中加入cef: .在BOOL CtestCEFApp::InitInstance()中初始化cef HINSTANCE hInst = GetModuleHandle(NULL); ...
- 10:处理 json
json 通用的数据类型, 所有的语言都认识.json 是字符串.key-value 必须使用双引号1. loads() 和 dumps() 的使用 json.loads() 将 json 字符串转换 ...
- Haskell语言学习笔记(83)Pipes
安装 pipes $ cabal install pipes Installed pipes-4.3.9 Prelude> import Pipes Prelude Pipes> impo ...
- 【379】pandas 说明
参考:Part 1 参考:Part 2
- form表单 获取与赋值
form表单中使用频繁的组件: 文本框.单选框.多选框.下拉框.文本域form通过getValues()获取表单中所有name的值 通过setValues({key:values})给对应的name值 ...
- Spark安装过程纪录
1 Scala安装 1.1 master 机器 修改 scala 目录所属用户和用户组. sudo chown -R hadoop:hadoop scala 修改环境变量文件 .bashrc , 添加 ...
- 19.Observales
然后 ng serve看看能不能启动 OK