) 打印存储 for k,vals in test_data: 断言集(finder(k,存储))==设置(vals) 再见, bearophile I have a two column list like:2,1316,3357,68,910,131131,995,10And I want to store it in a tree-like structure.So if I request 131, it should return all the child of 131, like 2, 10and 5 (since 5 is child of 10).If I request 335, it should return: 6 and 7.If I request 9, it should return 8.I guess I could use tuples or dictionaries to do it, but I can''t figure outhow.Best,SB.--Sebastián BassiDiplomado Ciencia y Tecnología.Club de la razón (www.clubdelarazon.org) 解决方案 On Apr 14, 9:37???am, "Sebastian Bassi" <[email protected]>wrote:I have a two column list like:2,1316,3357,68,910,131131,995,10And I want to store it in a tree-like structure.So if I request 131, it should return all the child of 131, like 2, 10and 5 (since 5 is child of 10).If I request 335, it should return: 6 and 7.If I request 9, it should return 8.I guess I could use tuples or dictionaries to do it, but I can''t figure out how.There are probably better ways.def tree_path(key,tree,indent):print ''\t''*indent,keyif tree.has_key(key):for m in tree[key]:tree_path(m,tree,indent+1)returnprint ''original data''printsource = [(2,131),(6,335),(7,6),(8,9),(10,131),(131,99),(5,1 0)]tree = {}for s in source:if tree.has_key(s[1]):tree[s[1]].append(s[0])else:tree[s[1]] = [s[0]]for t in tree:print ''%3d '' % (t),tree[t]printprinttree_path(99,tree,0)printprint ''extended data''printsource_extend = [(666,2),(777,2),(888,2)]for s in source_extend:if tree.has_key(s[1]):tree[s[1]].append(s[0])else:tree[s[1]] = [s[0]]for t in tree:print ''%3d '' % (t),tree[t]printprinttree_path(99,tree,0)## original data#### 99 [131]## 6 [7]## 9 [8]## 10 [5]## 335 [6]## 131 [2, 10]###### 99## 131## 2## 10## 5#### extended data#### 2 [666, 777, 888]## 99 [131]## 6 [7]## 9 [8]## 10 [5]## 335 [6]## 131 [2, 10]###### 99## 131## 2## 666## 777## 888## 10## 5>Best,SB.--Sebastián BassiDiplomado Ciencia y Tecnología.Club de la razón (www.clubdelarazon.org) Hope this helps# list of pairs [child,parent]list=[[2,131],[6,335],[7,6],[8,9],[10,131],[131,99],[5,10]]# list with loop#list=[[2,131],[6,335],[7,6],[8,9],[10,131],[131,99],[5,10],[3,10],[131,3]]# put the pairs in a dictionary, for easy retrievald={}for c,p in list:# must be able to hold multiple values for each keyif not(d.has_key(p)):d[p]=[c]else:d[p].append(c)# function to retrieve children using recursion. max_depth to ensureterminationdef retrieve_recursive(key,result=[],max_depth=10):if d.has_key(key) and max_depth>0:for i in d[key]:result.append(i)retrieve_recursive(i,result,max_depth-1)return resultprint retrieve_recursive(131) The solution I have found is quite similar to the ma********@gmail.comone (it uses a defaultdict, and it yields the result lazily), but itlacks the quite useful max_depth (it can be added):from collections import defaultdictdef finder(el, stor):if el in stor:for v in stor[el]:yield vfor v2 in finder(v, stor):yield v2data = [[131, 2], [335, 6], [6, 7], [9, 8], [131, 10], [99, 131], [10,5]]storage = defaultdict(list)for k, v in data:storage[k].append(v)test_data = ([131, [2, 10, 5]],[335, [6, 7]],[9, [8]],)print storagefor k, vals in test_data:assert set(finder(k, storage)) == set(vals)Bye,bearophile 这篇关于从2列列表中创建一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 04:51
查看更多