python實(shí)現(xiàn)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:
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]會(huì)出現(xiàn)異常
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中實(shí)現(xiàn)兩個(gè)字典(dict)合并的方法
- Python中如何優(yōu)雅的合并兩個(gè)字典(dict)方法示例
- Python中字典(dict)合并的四種方法總結(jié)
- python dict 相同key 合并value的實(shí)例
- python字典DICT類型合并詳解
- 對(duì)python 合并 累加兩個(gè)dict的實(shí)例詳解
- python兩種遍歷字典(dict)的方法比較
- python 字典(dict)遍歷的四種方法性能測(cè)試報(bào)告
- python遍歷 truple list dictionary的幾種方法總結(jié)
- python中dict字典的查詢鍵值對(duì) 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法
- python實(shí)現(xiàn)在遍歷列表時(shí),直接對(duì)dict元素增加字段的方法
- python實(shí)現(xiàn)兩個(gè)dict合并與計(jì)算操作示例
相關(guān)文章
python中open函數(shù)對(duì)文件處理的使用教程
open()函數(shù)的作用是打開一個(gè)文件,并返回一個(gè)file對(duì)象(即文件對(duì)象),下面這篇文章主要給大家介紹了關(guān)于python中open函數(shù)對(duì)文件處理的相關(guān)資料,需要的朋友可以參考下2022-06-06使用python檢測(cè)主機(jī)存活端口及檢查存活主機(jī)
這篇文章主要介紹了使用python檢測(cè)主機(jī)存活端口及檢查存活主機(jī)的相關(guān)資料,需要的朋友可以參考下2015-10-10python opencv 實(shí)現(xiàn)對(duì)圖像邊緣擴(kuò)充
今天小編就為大家分享一篇python opencv 實(shí)現(xiàn)對(duì)圖像邊緣擴(kuò)充,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01使用Python實(shí)現(xiàn)簡(jiǎn)單的爬蟲框架
爬蟲是一種自動(dòng)獲取網(wǎng)頁內(nèi)容的程序,它可以幫助我們從網(wǎng)絡(luò)上快速收集大量信息。下面我們將學(xué)習(xí)如何使用 Python 編寫一個(gè)簡(jiǎn)單的爬蟲框架,感興趣的可以了解一下2023-05-05