python实现dict版图遍历
python实现dict版图遍历的示例。
代码:
#_*_coding:utf_8_
import sys
import os
class Graph():
def __init__(self, V, E):
self.V = V
self.E = E
self.visited = []
self.dict = {}
self.fd = open("input.txt")
def initGraph(self):
self.visited = [0 for i in range(self.V+1)]
for i in range(self.E):
f, t = map(int, self.fd.readline().split())
#f, t = map(int, sys.stdin.readline().split())
if self.dict.has_key(f)==False:
# www.jbxue.com
l = []
l.append(t)
self.dict[f] = l
else:
l = self.dict[f]
l.append(t)
self.dict[f] = l
def dfsGraph(self, src):
self.visited[src] = 1
print src ,
if self.dict.get(src): #self.dict[src]会出现异常
for u in self.dict[src]:
if self.visited[u]==0:
self.dfsGraph(u)
graph = Graph(6, 10)
graph.initGraph()
graph.dfsGraph(1)
nput.txt
代码:
1 2
1 3
1 4
3 2
2 6
4 3
3 5
4 5
6 5
3 6
output:
代码如下:
1 2 6 5 3 4
python实现dict版图遍历的更多相关文章
- Python中dict的特点、更新dict、遍历dict
dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...
- Python tricks(3) -- list和dict的遍历和方法
每个人在使用python的过程中都会遍历list和dict. List遍历 最常用最简单的遍历list的方法 a = ["a", "b", "c&qu ...
- Python中dict详解
from:http://www.cnblogs.com/yangyongzhi/archive/2012/09/17/2688326.html Python中dict详解 python3.0以上,pr ...
- python 字典 dict 该注意的一些操作
在用python处理dict 的时候,有几个该注意的地方,这里跟大家提一下: 1)操作dict 时,尽量少产生新的列表对象.比如: 遍历dict的时候,如果用 dic = {"a" ...
- Python 基础 Dict 和 Set 类型
python 什么是dict 例如: d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } 我们把名称称为key,对应的成绩称为value,dic就是通过key 来查找 ...
- Python之Dict和Set类型(入门5)
转载请标明出处: http://www.cnblogs.com/why168888/p/6407905.html 本文出自:[Edwin博客园] Python之Dict和Set类型 1. Python ...
- Python的dict字典结构操作方法学习笔记
Python的dict字典结构操作方法学习笔记 这篇文章主要介绍了Python的dict字典结构操作方法学习笔记本,字典的操作是Python入门学习中的基础知识,需要的朋友可以参考下 一.字典的基本方 ...
- python 字典dict - python基础入门(15)
前面的课程讲解了字符串str/列表list/元组tuple,还有最后一种比较重要的数据类型也需要介绍介绍,那就是python字典,俗称:dict. python中的字典可与字符串/列表/元组不同,因为 ...
- Python的dict
dict把key和value关联起来,可以通过 key来查找 value. 花括号 {} 表示这是一个dict,然后按照 key: value, 写出来即可.最后一个 key: value 的逗号可以 ...
随机推荐
- springBoot-自定义监听器
package com.cx.springboot.mylistener; import org.springframework.boot.context.event.ApplicationReady ...
- Appium+python自动化19-iOS模拟器(iOS Simulator)安装自家APP
前言 做过iOS上app测试的小伙伴应该都知道,普通用户安装app都是从appstore下载安装,安装测试版本的app,一般就是开发给的二维码扫码安装, 或者开发给个.ipa的安装包文件,通过itoo ...
- Android 交叉编译程序提示(not found)
原因是缺少库文件, 解决办法:arm-linux-readelf -a helloword | grep NEEDED 拷贝so文件到安卓下 或者 arm-linux-gcc hello.c -o h ...
- Shell编程(脚本)的经常使用命令和语句
一些经常使用的Shell编程(脚本)命令和语句,能够满足一般需求. 接收到的命令參数: 參数个数: $# 參数值: 命令本身:$0 第一个參数:$1 第二个參数:$2 -- 退出命令: exit ec ...
- JS辨别访问浏览器
项目中需要扫描二维码之后自动分辨出是android还是ios系统,针对于不同的系统进行不同的下载. <script type="text/javascript"> /* ...
- Ubuntu 16.04 LTS软件包管理基本操作
前文 Ubuntu 16.04 新特性中我们已经介绍过,随着 Ubuntu 16.04 LTS 的发布,Ubuntu 的软件包管理命令也发生了变化,新系统采用了 Debian 项目中所使用的 APT( ...
- 【转】javascript 中的很多有用的东西
原文:https://www.cnblogs.com/ys-ys/p/5158510.html ---------------------------------------------------- ...
- ORCU浅析之安装和作用
众所周知,在安装Oracle BIEE之前需要安装Oracle RCU(Oracle Repository Creation Utility),美其名曰资料档案库.如果单单的从名称上来说,那就是我们实 ...
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树全部左右子数 ...