我的python程序返回一个包含子列表数据的列表。每个子列表包含项目的唯一id和该项目的父id,即
pages_id_list ={ {22, 4},{45,1},{1,1}, {4,4},{566,45},{7,7},{783,566}, {66,1},{300,8},{8,4},{101,7},{80,22}, {17,17},{911,66} }在每个子列表中,数据都是以{*article_id*, *parent_id*}的方式构造的,如果article_id和parent_id是相同的,那么这显然意味着文章没有父级。
我希望使用最少的代码对数据进行排序,以便在每一篇文章中,我可以很容易地访问它的子代和孙子(嵌套数据)的列表(如果可用的话)。例如(使用上面的示例数据),我应该能够在一天结束时打印:
1
-45
--566
---783
-66
--911……关于第id条1
我只能整理出最高级别(第一代和第二代) ids。有问题的第三代和以后的几代人。
这是我使用的代码:
highest_level = set()
first_level = set()
sub_level = set()
for i in pages_id_list:
id,pid = i['id'],i['pid']
if id == pid:
#Pages of the highest hierarchy
highest_level.add(id)
for i in pages_id_list:
id,pid = i['id'],i['pid']
if id != pid :
if pid in highest_level:
#First child pages
first_level.add(id)
else:
sub_level.add(id)很遗憾,我的代码不起作用。
如能在正确的方向上提供任何帮助/推动,将不胜感激。谢谢
大卫
发布于 2013-01-07 03:16:24
也许是这样的:
#! /usr/bin/python3.2
pages_id_list = [ (22, 4),(45,1),(1,1), (4,4),(566,45),(7,7),(783,566), (66,1),(300,8),(8,4),(101,7),(80,22), (17,17),(911,66) ]
class Node:
def __init__ (self, article):
self.article = article
self.children = []
self.parent = None
def print (self, level = 0):
print ('{}{}'.format ('\t' * level, self.article) )
for child in self.children: child.print (level + 1)
class Tree:
def __init__ (self): self.nodes = {}
def push (self, item):
article, parent = item
if parent not in self.nodes: self.nodes [parent] = Node (parent)
if article not in self.nodes: self.nodes [article] = Node (article)
if parent == article: return
self.nodes [article].parent = self.nodes [parent]
self.nodes [parent].children.append (self.nodes [article] )
@property
def roots (self): return (x for x in self.nodes.values () if not x.parent)
t = Tree ()
for i in pages_id_list: t.push (i)
for node in t.roots: node.print ()这将创建一个树结构,您可以遍历该树结构以获取所有子项。您可以通过t.nodes [article]访问任何文章,并通过t.nodes [article].children获取其子类。
打印方法的输出是:
1
45
566
783
66
911
4
22
80
8
300
7
101
17发布于 2013-01-07 03:05:35
下面是一种简单的方法(假设您的页面id列表元素没有设置,正如您的代码所建议的):
from collections import defaultdict
page_ids = [
(22, 4), (45, 1), (1, 1), (4, 4),
(566, 45), (7, 7), (783, 566), (66, 1), (300, 8),
(8, 4), (101, 7), (80, 22), (17, 17), (911, 66)
]
def display(id, nodes, level):
print('%s%s%s' % (' ' * level, '\\__', id))
for child in sorted(nodes.get(id, [])):
display(child, nodes, level + 1)
if __name__ == '__main__':
nodes, roots = defaultdict(set), set()
for article, parent in page_ids:
if article == parent:
roots.add(article)
else:
nodes[parent].add(article)
# nodes now looks something like this:
# {1: [45, 66], 66: [911], 4: [22, 8], 22: [80],
# 7: [101], 8: [300], 45: [566], 566: [783]}
for id in sorted(roots):
display(id, nodes, 0)产出将是:
\__1
\__45
\__566
\__783
\__66
\__911
\__4
\__8
\__300
\__22
\__80
\__7
\__101
\__17发布于 2013-01-07 04:05:08
我想用最小的代码对数据进行排序。
我阅读到现在为止,因此我将提供另一个答案。我不会编辑我以前的答案,因为它们真的没有关系。如果您想用最少的代码将元组列表转移到树结构中,那么这种方法是非常小的,尽管它仍然可以被进一步最小化(例如使用递归lambda项而不是函数):
pages_id_list = [ (22, 4),(45,1),(1,1), (4,4),(566,45),(7,7),(783,566), (66,1),(300,8),(8,4),(101,7),(80,22), (17,17),(911,66) ]
def getTree (item, pages): return [ (x, getTree (x, pages) ) if getTree (x, pages) else x for x in (x [0] for x in pages if x [1] == item) ]
tree = getTree (None, [ (x [0], None if x [0] == x [1] else x [1] ) for x in pages_id_list] )https://stackoverflow.com/questions/14189114
复制相似问题