从2列列表中创建一棵树 [英] Making a tree out of a 2 column list

查看:73
本文介绍了从2列列表中创建一棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个列列表如下:


2,131

6,335

7,6

8,9

10,131

131,99

5,10


我我希望将它存储在树状结构中。

因此,如果我要求131,它应该返回131的所有子项,如2,10,
和5(从那以后) 5是10)的孩子。

如果我要求335,它应该返回:6和7.

如果我要求9,它应该返回8.

我想我可以使用元组或词典来实现它,但我无法想象。


最好,

SB。< br $>
-
$ b $bSebastiánBassi

Diplomado CienciayTecnología。

Club delarazón( www.clubdelarazon.org

解决方案

< blockquote> 4月14日9:37 ??? am,Sebastian Bassi < sba ... @ clubdelarazon.org>

写道:


我有两列列表,如:


2,131

6,335

7,6

8,9

10,131

131,99

5,10


我想把它存放在树状结构中。

因此,如果我要求131,它应该返回131的所有孩子,例如2,10

和5(因为5是10岁的孩子)。

如果我要求335,它应该返回:6和7.

如果我要求9,它应该返回8.

我想我可以使用元组或字典来做到这一点,但我不知道怎么做。



可能有更好的方法。


def tree_path(密钥,树,缩进):

打印''\t''*缩进,键

如果tree.has_key(键):

for m in tree [key]:

tree_path(m,树,缩进+ 1)

返回


打印''原始数据''

打印


来源= [(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]]

树中的


打印''%3d''%(t),树[t]


打印

打印

tree_path(99,树,0)


print

打印''扩展数据''

打印


source_extend = [(666,2),(777,2),( 888,2)]

对于source_extend中的s


if tree.has_key(s [1]):

tree [S [1]]。追加(s [0])

否则:

树[s [1]] = [s [0]]


for tree:

打印''%3d''%(t),树[t]


打印

打印

tree_path(99,树,0)

##原始数据

##

## 99 [131]

## 6 [7]

## 9 [8]

## 10 [5]

## 335 [6]

## 131 [2,10]

##

##

## 99

## 131

## 2

## 10

## 5

##

##扩展数据

##

## 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


>

最好,

SB。


-
$ b $bSebastiánBassi

Diplomado CienciayTecnología。

Club delarazón( www.clubdelarazon.org



希望这有帮助


#对子列表[孩子,父母]

list = [[2,131],[6,335],[7,6],[8,9],[10,131],[131,99], [5,10]]


#循环列表

#list = [[2,131],[6,335],[7,6],[8 ,9],[10,131],[131,99],[5,10],[3,10],

[131,3]]


#将这些对放入字典中,以便于检索

d = {}

for c,p in list:

#必须是能够为每个键保持多个值

如果不是(d.has_key(p)):

d [p] = [c]

否则:

d [p] .append(c)

#函数用于使用递归检索子项。 max_depth确保

终止

def retrieve_recursive(key,result = [],max_depth = 10):
如果d.has_key(键)
和max_depth> 0:
我在d [key]中的


result.append(i)

retrieve_recursive(i,result,max_depth -1)

返回结果


print retrieve_recursive(131)


我发现的解决方案与 ma********@gmail.com
一个(它使用一个defaultdict,它会懒散地产生结果),但是它没有相当有用的max_depth(可以添加)$

来自收藏品导入defaultdict


def finder(el,stor):

如果el存储:

for v in stor [el]:

yield v

for v2 in finder(v,stor):

yield v2


data = [[131,2],[335,6],[6,7],[9,8],[131,10],[99,131],[10,

5]]


存储=默认dict(list)

for k,v in data:

storage [k] .append(v)


test_data = ([131,[2,10,5]],

[335,[6,7]],

[9,[8]],



打印存储

for k,vals in test_data:

断言集(finder(k,存储))==设置(vals)


再见,

bearophile


I have a two column list like:

2,131
6,335
7,6
8,9
10,131
131,99
5,10

And 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, 10
and 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 Bassi
Diplomado Ciencia y Tecnología.
Club de la razón (www.clubdelarazon.org)

解决方案

On Apr 14, 9:37???am, "Sebastian Bassi" <sba...@clubdelarazon.org>
wrote:

I have a two column list like:

2,131
6,335
7,6
8,9
10,131
131,99
5,10

And 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, 10
and 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,key
if tree.has_key(key):
for m in tree[key]:
tree_path(m,tree,indent+1)
return

print ''original data''
print

source = [(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]

print
print
tree_path(99,tree,0)

print
print ''extended data''
print

source_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]

print
print
tree_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 Bassi
Diplomado 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 retrieval
d={}
for c,p in list:
# must be able to hold multiple values for each key
if not(d.has_key(p)):
d[p]=[c]
else:
d[p].append(c)
# function to retrieve children using recursion. max_depth to ensure
termination
def 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 result

print retrieve_recursive(131)


The solution I have found is quite similar to the ma********@gmail.com
one (it uses a defaultdict, and it yields the result lazily), but it
lacks the quite useful max_depth (it can be added):

from collections import defaultdict

def finder(el, stor):
if el in stor:
for v in stor[el]:
yield v
for v2 in finder(v, stor):
yield v2

data = [[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 storage
for k, vals in test_data:
assert set(finder(k, storage)) == set(vals)

Bye,
bearophile


这篇关于从2列列表中创建一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆