[python] python 便利单向graph
------------恢复内容开始------------
#import collections
from collections import deque
# 想象成指针,
graph={}
graph['you']=['alice','bob','claire']
graph['bob']=['anuj','peggy']
graph['alice']=['peggy']
graph['claire']=['thom','jonnym']
graph['anuj']=[]
graph['peggy']=[]
graph['thom']=[]
graph['jonnym']=[]
print ('starting')
def search_mango_seller_from(name):
search_queue=deque()
search_queue += graph[name]
searchd=[]
#print(*graph['you'])
#'''
while search_queue:
person = search_queue.popleft()
if person_is_seller(person):
print (person + ' is a mango seller!')
print ('--True--')
else:
search_queue += graph[person]
searchd.append(person)
print('--False --')
#'''
def person_is_seller(name):
return name[-1] == 'm'
search_mango_seller_from('you')
------------恢复内容结束------------

浙公网安备 33010602011771号