如何为 python AnyTree 模块采用逗号分隔的输入?

How can I take comma separated inputs for python AnyTree module?

社区。我需要接受多个以逗号分隔的输入以生成信息摘要(具体来说,每个 group/project 中有多少不同的员工参与)?该程序以字符串形式获取员工、经理和组。

我正在使用 anytree python 库来 search/count 每个组中每个员工的出现。但是,该程序一次只接受一个 value/cell 而不是多个值。

这是树结构以及我如何接受输入值?

Press q to exit, Enter your data: Joe
Press q to exit, Enter your data: Manager1
Press q to exit, Enter your data: Group1
Press q to exit, Enter your data: Charles 
Press q to exit, Enter your data: Manager1
Press q to exit, Enter your data: Group2
Press q to exit, Enter your data: Joe
Press q to exit, Enter your data: Manager3
Press q to exit, Enter your data: Group1
Press q to exit, Enter your data: Charles
Press q to exit, Enter your data: Manager3
Press q to exit, Enter your data: Group1
Press q to exit, Enter your data: Joe
Press q to exit, Enter your data: Manager5
Press q to exit, Enter your data: Group2
Press q to exit, Enter your data: q
Employee   No of groups
   JOE       2
   CHARLES       2
Group
├── GROUP1
│   ├── JOE
│   │   └── MANAGER1
│   ├── JOE
│   │   └── MANAGER3
│   └── CHARLES
│       └── MANAGER3
└── GROUP2
    ├── CHARLES
    │   └── MANAGER1
    └── JOE
        └── MANAGER5

我需要有关此代码的帮助,以便它可以接受逗号分隔值;例如,一次输入 Joe、Manager1、Group1

import anytree

from anytree import Node, RenderTree, LevelOrderIter, LevelOrderGroupIter, PreOrderIter

import sys

# user input
io=''
lst_input = []
while (io!='q'):
    io=input('Press q to exit, Enter your data: ')
    if io!='q':
        lst_input.append(io.upper())

# change list in to matrix
lst=[]
for i in range(0, len(lst_input), 3):
    lst.append(lst_input[i:i + 3])

lst

# create tree structure from lst
group = Node('Group')
storeGroup = {}
for i in range(len(lst)):
    if lst[i][2] in [x.name for x in group.children]: # parent already exist, append childrens
        storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]])
        storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]])
    else: # create parent and append childreds
        storeGroup[lst[i][2]] = Node(lst[i][2], parent=group)
        storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]])
        storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]])


store = {}
for children in LevelOrderIter(group, maxlevel=3):
    if children.parent!=None and children.parent.name!='Group':
        if children.name not in store:
            store[children.name] = {children.parent.name}
        else:
            store[children.name] = store[children.name] | {children.parent.name}

print('Employee', '  No of groups')
for i in store:
    print('   '+i+'      ', len(store[i]))


for pre,fill, node in RenderTree(group):
    print('{}{}'.format(pre,node.name))


谢谢!欢迎任何想法。

我相信一种解决方法是:

name, role, grp = io.upper().split(',')

对于 Joe, Manager1, Group1

这样的输入
Python 3.7.9 (v3.7.9:13c94747c7, Aug 15 2020, 01:31:08)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> name, role, grp = input("Press q to exit, Enter your data:").split(",")
Press q to exit, Enter your data:Joe, Manager1, Group1
>>> name
'Joe'
>>> role
' Manager1'
>>> grp
' Group1'

这对你有用吗?

利用解包来提取元素。那么if语句可以这样re-written

if io!='q':
    name, role, grp = io.upper(). split(',')
    lst_input.append([name,role, grp]) 

您还需要将 for 循环中的 lst.append(lst_input[i:i + 3]) 更改为此。

lst.append(lst_input[0][i:i + 3])

您的 lst 输出类似于:

[['Joe', 'Manager1', 'Group1'], ['Charles', 'Manager1', 'Group2'], ['Joe', 'Manager3', 'Group1'], ['Charles', 'Manager3', 'Group1'], ['Joe', 'Manager5', 'Group2']]

下面的代码生成相同的 lst 输出,但性能更好:

import anytree

from anytree import Node, RenderTree, LevelOrderIter, LevelOrderGroupIter, PreOrderIter

import sys

# user input
lst = []
while True:
   io=input('Press q to exit, Enter your data: ')
   if io!='q':
      lst.append(io.upper().split(',')) 
   else:
      break
print(lst)

# create tree structure from lst
group = Node('Group')
storeGroup = {}
for i in range(len(lst)):
    if lst[i][2] in [x.name for x in group.children]: # parent already exist, append childrens
        storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]])
        storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]])
    else: # create parent and append childreds
        storeGroup[lst[i][2]] = Node(lst[i][2], parent=group)
        storeGroup[lst[i][0]] = Node(lst[i][0], parent=storeGroup[lst[i][2]])
        storeGroup[lst[i][1]] = Node(lst[i][1], parent=storeGroup[lst[i][0]])


store = {}
for children in LevelOrderIter(group, maxlevel=3):
    if children.parent!=None and children.parent.name!='Group':
        if children.name not in store:
            store[children.name] = {children.parent.name}
        else:
            store[children.name] = store[children.name] | {children.parent.name}

print('Employee', '  No of groups')
for i in store:
    print('   '+i+'      ', len(store[i]))


for pre,fill, node in RenderTree(group):
    print('{}{}'.format(pre,node.name))

在您的原始代码中定义大量不必要的列表会破坏您的性能。甚至避免进行额外的比较或定义不必要的变量。请注意,我们在 Python 中没有 do ... while 结构。所以,我们使用 break 来避免重复比较。