2

私の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日の終わりに印刷できるはずです。

 1
 -45
 --566
 ---783
 -66
 --911

....記事ID1の場合

最高レベル(第1世代と第2世代)のIDしか分類できませんでした。3世代目以降の取得に問題があります。

これは私が使用したコードです:

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)

私のコードは悲しいことに機能しません。

正しい方向への助け/微調整は大歓迎です。ありがとう

デビッド

4

3 に答える 3

5

多分このようなもの:

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

printメソッドの出力は次のとおりです。

1
    45
        566
            783
    66
        911
4
    22
        80
    8
        300
7
    101
17
于 2013-01-07T03:16:24.290 に答える
1

簡単なアプローチは次のとおりです(コードが示すように、ページ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

ソース:https ://gist.github.com/4472070

于 2013-01-07T03:05:35.447 に答える
1

最小限のコードでデータを並べ替えたい

私はこれまで読んだので、別の答えを提供します。以前の回答は実際には関連していないため、編集しません。タプルのリストを最小限のコードでツリー構造に転送する場合、このアプローチは非常に最小限ですが、さらに最小化することもできます(たとえば、関数の代わりに再帰的なラムダ項を使用する)。

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] )
于 2013-01-07T04:05:08.473 に答える