Форум сайта python.su
имею код, рекурсия..должна в цикле для каждого узла графа находить все связи..
graph={'673TCDealersBillingAgregationByPaymntresp': ['562TCDealersBillingIbonus', '632TCDealersBillingAgregationByOutlets'],
'616TCDealersBillingObonus': ['496TCDealersBillingBillingRecordCreation', '572TCDealersBillingLbonus'],
'475TCDealersBillingInsertContracts': ['592TCDealersBillinggmbdTwithdrawal', '620TCDealersBillingRbonus'],
'562TCDealersBillingIbonus': ['594TCDealersBillinggmbdIwithdrawal', '576TCDealersBillinggmbacTmNwithdrawal', '500TCDealersBillingDataArchiving'],
'582TCDealersBillinggmbacTmIwithdrawal': ['550TCDealersBillingMbonus', '560TCDealersBillingTbonus', '477TCDealersBillingBuildHierarchy', '534TCDealersBillingGetMoneyBackNotPayed', '475TCDealersBillingInsertContracts', '600TCDealersBillingGbonus'],
'511TCDealersBillingDbonus': ['502TCDealersBillingInvoiceCreation', '570TCDealersBillingNbonus', '642TCDealersBillingServCenterBonus', '673TCDealersBillingAgregationByPaymntresp'],
'500TCDealersBillingDataArchiving': ['502TCDealersBillingInvoiceCreation'],
'634TCDealersBillingMarketing': ['634TCDealersBillingMarketing', '612TCDealersBillingKbonus'],
'554TCDealersBillinggmbacTmTwithdrawal': ['511TCDealersBillingDbonus', '614TCDealersBillingSbonus']}
import sys
report=sys.stdout#open("123.txt","w")
def find_all_paths(graph, start, val=[], path=[], a=[]):
path = path + [start]
val.append(path)
if not graph.has_key(start):
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, val, path, a)
for newpath in newpaths:
paths.append(newpath)
for i in val:
for j in i:
if j not in a:
a.append(j)
a.remove(start)
return a
print>>report, find_all_paths(graph,'511TCDealersBillingDbonus')
print>>report, "===================================================="
print>>report, find_all_paths(graph,'602TCDealersBillingJbonus')
print>>report, "++++++++++++++++++++++++++++++++++++++++++++++++"
print>>report, find_all_paths(graph,'511TCDealersBillingDbonus')
print>>report, "===================================================="
['502TCDealersBillingInvoiceCreation', '570TCDealersBillingNbonus', '642TCDealersBillingServCenterBonus', '594TCDealersBillinggmbdIwithdrawal', '576TCDealersBillinggmbacTmNwithdrawal', '500TCDealersBillingDataArchiving', '562TCDealersBillingIbonus', '632TCDealersBillingAgregationByOutlets', '673TCDealersBillingAgregationByPaymntresp']
====================================================
[]
++++++++++++++++++++++++++++++++++++++++++++++++
['502TCDealersBillingInvoiceCreation', '570TCDealersBillingNbonus', '642TCDealersBillingServCenterBonus', '594TCDealersBillinggmbdIwithdrawal', '576TCDealersBillinggmbacTmNwithdrawal', '632TCDealersBillingAgregationByOutlets',[u]602TCDealersBillingJbonus[/u], '500TCDealersBillingDataArchiving', '562TCDealersBillingIbonus', '673TCDealersBillingAgregationByPaymntresp']
Отредактировано (Май 28, 2009 00:02:50)
Офлайн
поменять сигнатуру.
на def find_all_paths(graph, start, val=None, path=None, a=None)
внутри
if val is None:
val =
с mutable default values легко поймать side-effect, что и произошло.
Иллюстрация:
>>> def f(a, b=[]):
... b.append(a)
... return b
...
>>> f(1, [])
[1]
>>> f(1, [])
[1]
>>> f(2, [])
[2]
>>> f(2, [1])
[1, 2]
>>> f(1)
[1]
>>> f(1)
[1, 1]
>>> f(2)
[1, 1, 2]
>>>
Офлайн
спасибо..помогло..не знал что может произойти такой эффект..
Офлайн
default values считаются на момент СОЗДАНИЯ функции, а не на момент ее ВЫЗОВА
Офлайн