python pid/id 分组得到如何得到制定格式的数据?

新手上路,请多包涵

我知道用递归,但是就是写不出来,
相关问题链接:https://segmentfault.com/q/10...
原始数据

1 My Documents 0
2 photos 1
3 Friend 2
4 Wife 2
5 Company 2
6 Program Files 1
7 Intel 6
8 Java 6

原始数据格式

(
    {
        'id': 1,
        'name': 'My Documents',
        'pid': 0
     },
    {
        'id': 2,
        'name': 'photos ',
        'pid': 1
     },
    {
        'id': 3,
        'name': 'Friend',
        'pid': 2
     },
    {
        'id': 4,
        'name': 'Wife',
        'pid': 2
     },
    {
        'id': 5,
        'name': 'Company',
        'pid': 2
     },
    {
        'id': 6,
        'name': 'Program Files',
        'pid': 1
    },
    {
        'id': 8 ,
        'name': 'Java ',
        'pid': 6
    },
)

得到目标数据:
将name关联依次用 ‘_’ 链接直到尾节点,例如下面

[
    {
        'id': 2,
        'name': 'My Documents_ photos',
    },
    {
        'id': 8,
        'name': 'My Documents_Program Files_java'
    },
    ...
]
阅读 4.2k
4 个回答
def find(arr,id):
    for idx, e in enumerate(arr):
        if e['id'] == id:
            return idx
    return -1
    
def path(arr, n):
    print(n)
    if arr[n]['pid'] == 0:
        return arr[n]['name']
    else:
        return path(arr, find(arr, arr[n]['pid'])) + '_' + arr[n]['name']
        
def generate(arr,n):
    return {'id':n, 'name':path(arr,find(arr,n))}
 generate(arr,6) #{'id': 6, 'name': 'My Documents_Program Files'}

面向对象的, 可以再封装

raw = \
(
    {
        'id': 1,
        'name': 'My Documents',
        'pid': 0
     },
    {
        'id': 2,
        'name': 'photos ',
        'pid': 1
     },
    {
        'id': 3,
        'name': 'Friend',
        'pid': 2
     },
    {
        'id': 4,
        'name': 'Wife',
        'pid': 2
     },
    {
        'id': 5,
        'name': 'Company',
        'pid': 2
     },
    {
        'id': 6,
        'name': 'Program Files',
        'pid': 1
    },
    {
        'id': 8 ,
        'name': 'Java ',
        'pid': 6
    },
)


class Node:
    def __init__(self, id, name, pid):
        self.id = id
        self.name = name
        self.pid = pid
        self.children = []
        self.parent = None

    def __lt__(self, other):
        if self.pid < other.pid:
            return True
        elif self.pid == other.pid and self.id < other.id:
            return True
        return False

    @staticmethod
    def print_path(n):
        path = n.name
        while n.parent:
            path = n.parent.name + '_' + path
            n = n.parent
        print(path)

nodes = [Node(**_) for _ in raw]
nodes.sort()
tmp = {}

for node in nodes:
    tmp[node.id] = node
    if node.pid in tmp:
        tmp[node.pid].children.append(node)
        node.parent = tmp[node.pid]


for node in nodes:
    Node.print_path(node)
    
'''
My Documents
My Documents_photos 
My Documents_Program Files
My Documents_photos _Friend
My Documents_photos _Wife
My Documents_photos _Company
My Documents_Program Files_Java 
'''

'''
将数据生成递归结果集
'''

!/usr/bin/python3

-- coding: UTF-8 --

arr = (

{
    'id': 1,
    'name': 'My Documents',
    'pid': 0
},
{
    'id': 2,
    'name': 'photos ',
    'pid': 1
},
{
    'id': 3,
    'name': 'Friend',
    'pid': 2
},
{
    'id': 4,
    'name': 'Wife',
    'pid': 2
},
{
    'id': 5,
    'name': 'Company',
    'pid': 2
},
{
    'id': 6,
    'name': 'Program Files',
    'pid': 1
},
{
    'id': 8,
    'name': 'Java ',
    'pid': 6
},

)

def get_node(arr, id):

for node in arr:
    if node['id'] == id:
        if node['pid'] == 0:
            return node['name']
        else:
            return get_node(arr, node['pid']) + '_' + node['name']
else:
    return ''

for i in arr:

node = {'id': i['id'], 'name': get_node(arr, i['id'])}
print(node)
新手上路,请多包涵
raw= (
    {
        'id': 1,
        'name': 'My Documents',
        'pid': 0
     },
    {
        'id': 2,
        'name': 'photos ',
        'pid': 1
     },
    {
        'id': 3,
        'name': 'Friend',
        'pid': 2
     },
    {
        'id': 4,
        'name': 'Wife',
        'pid': 2
     },
    {
        'id': 5,
        'name': 'Company',
        'pid': 2
     },
    {
        'id': 6,
        'name': 'Program Files',
        'pid': 1
    },
    {
        'id': 8 ,
        'name': 'Java ',
        'pid': 6
    },
)
def getname(id,btr):
  for a in raw:
    if a["id"] == id:
      if a["pid"] == 0:
          btr = a["name"] + "_" + btr
          print btr.strip("_")
      else:
          btr = a["name"] + "_" + btr
          getname(a["pid"], btr)


for b in raw:
  btr = ""
  getname(b["id"], btr)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题