如何在阵列中正确打印所有信息?

时间:2019-02-12 14:06:45

标签: python arrays list nodes

我想从所有所谓的“节点”中打印出所有信息,当我要遍历所有“节点”时,我不知道要指定什么。重要的是,它可以与我想要的节点数一起使用,

我尝试了几乎所有我能想到的事情,例如仅将它们命名为数字,并尝试使用该数字将它们“定位”在数组中。

List_of_Nodes = {}
List_of_Names = []


class Nodes:
    def __init__(self, nodeID, parentID):
        self.nodeID = str(nodeID)
        self.parentID = parentID

    def __str__(self):
        return '\nnodeID: {} \nParentID: {} '.format(self.nodeID,self.parentID)

Num_Nodes = int(input("The number of nodes: "))
for n in range(Num_Nodes):
    if n == 0:
        new_nodeID = input("\nPlease enter a name: ")
    else:
        new_nodeID = input("Please enter another name: ")
    List_of_Names.append(new_nodeID)
    Num_Parents = int(input("the number of parents for this node"))
    for n2 in range(Num_Parents):
        if n2 == 0:
            new_parentID = input("Please enter a parentName: ")
        else:
            new_parentID = input("Please enter another parentName:")
        List_of_Nodes[new_nodeID, new_parentID] = Nodes(new_nodeID, new_parentID)

for List_of_Nodes in List_of_Nodes:
    print(Nodes(List_of_Nodes, List_of_Nodes))

这就是我输入的内容:

The number of nodes: 2

Please enter a name: me
the number of parents for this node2
Please enter a parentName: father
Please enter another parentName:mother

Please enter another name: sister
the number of parents for this node2
Please enter a parentName: father
Please enter another parentName:mother

这是输出

nodeID: ('me', 'father') 
ParentID: ('me', 'father') 

nodeID: ('me', 'mother') 
ParentID: ('me', 'mother') 

nodeID: ('sister', 'father') 
ParentID: ('sister', 'father') 

nodeID: ('sister', 'mother') 
ParentID: ('sister', 'mother') 

我希望它输出什么:

nodeID: me
ParentID: father, mother 

nodeID: sister
ParentID: father, mother

0 个答案:

没有答案