简体   繁体   English

使用条件节点链接迭代链表

[英]Iterate over linked list with conditional node links

I am very new to the idea of coding so I'm not 100% sure what I'm asking.我对编码的想法很陌生,所以我不能 100% 确定我在问什么。 I have a Object list that has the ID of a box, The amount of selectable options and the ID's of the boxes its linked to.我有一个对象列表,其中包含一个框的 ID、可选选项的数量以及与其链接的框的 ID。

I'm trying iterate in order across these boxes adding up all the possible options that can be selected我正在尝试在这些框中按顺序迭代添加所有可以选择的选项在此处输入图片说明

For example "Connect 6" would have a total of 3, "contect 7" a total of 2 and, "Connect 4" a total of 7例如,“Connect 6”总共有 3 个,“contect 7”总共有 2 个,“Connect 4”总共有 7 个

I have 2 classes 1 that sets up all the links in a list and 1 that then should iterate across them.我有 2 个类 1 设置列表中的所有链接,然后应该遍历它们的 1 个类。 I haven't been able to get to the sum part yet as whatever I make prints out the fist box ID forever till it crashes VS (No idea how to stop it doing my dumb shit once its started going, even closing the terminal doesn't help)我还没有能够得到总和部分,因为我所做的任何事情都会永远打印出拳头盒 ID,直到它崩溃 VS(不知道如何在它开始运行后阻止它做我的蠢事,即使关闭终端也不会)帮助)

class Node:
    def __init__(self, box_id):
        self.box_id = box_id
        self.options_number = None
        self.pre = None
        self.next = None
    def __repr__(self):
        return self.box_id

class LinkedList():
    def __init__(self, nodes=None):
        self.head = None
        self.reg_num = 0
        if nodes is not None:
            for node in nodes:
                box_id = node.box_id
                # print(f'One: {box_id}')
                # box_id = node.next
                # print(f'Two: {box_id}')
                # if box_id == []:
                #     print("I should really stop")
                while box_id != []:
                    print(box_id)
                    box_id = node.next

Here is what it looks like printing out the node.这是打印出节点的样子。

nodes = node_setup(test)
for node in nodes:
   print(f'Question Text: {node.name}  Current: {node.box_id} Options: {node.options_number} Next Link: {node.next}')
Question Text: Connect 1  Current: {30706B51-789C-4AFA-998D-759297B95F99} Options: 1 Next Link: ['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
Question Text: Connect 2  Current: {EA65B6DA-90FE-412A-AF1F-83F994CCB5FA} Options: 3 Next Link: ['{77AC8029-95D8-4DA8-9B62-D487DBA742D6}']
Question Text: Connect 3  Current: {77AC8029-95D8-4DA8-9B62-D487DBA742D6} Options: 1 Next Link: ['{DF56E1B2-DD19-485D-B720-A5A28AA6C156}']
Question Text: Connect 5  Current: {B4045D8D-6E54-4E43-B820-4C70036F08AF} Options: 1 Next Link: ['{D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5}']
Question Text: Connect 4  Current: {DF56E1B2-DD19-485D-B720-A5A28AA6C156} Options: 2 Next Link: ['{B4045D8D-6E54-4E43-B820-4C70036F08AF}', '{8C51E3B4-7302-4453-AEE0-7C9E659CA2B0}']
Question Text: Connect 6  Current: {8C51E3B4-7302-4453-AEE0-7C9E659CA2B0} Options: 2 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 7  Current: {D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5} Options: 1 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 8  Current: {D837ECDC-897D-4A91-89E3-F6222FE12F06} Options: 1 Next Link: []

And here it is running LinkedList在这里它正在运行 LinkedList

for node in nodes:
    print(f'Question Text: {node.name}  Current: {node.box_id} Options: {node.options_number} Next Link: {node.next}')
plz_Work = LinkedList(nodes)
Question Text: Connect 1  Current: {30706B51-789C-4AFA-998D-759297B95F99} Options: 1 Next Link: ['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
Question Text: Connect 2  Current: {EA65B6DA-90FE-412A-AF1F-83F994CCB5FA} Options: 3 Next Link: ['{77AC8029-95D8-4DA8-9B62-D487DBA742D6}']
Question Text: Connect 3  Current: {77AC8029-95D8-4DA8-9B62-D487DBA742D6} Options: 1 Next Link: ['{DF56E1B2-DD19-485D-B720-A5A28AA6C156}']
Question Text: Connect 5  Current: {B4045D8D-6E54-4E43-B820-4C70036F08AF} Options: 1 Next Link: ['{D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5}']
Question Text: Connect 4  Current: {DF56E1B2-DD19-485D-B720-A5A28AA6C156} Options: 2 Next Link: ['{B4045D8D-6E54-4E43-B820-4C70036F08AF}', '{8C51E3B4-7302-4453-AEE0-7C9E659CA2B0}']
Question Text: Connect 6  Current: {8C51E3B4-7302-4453-AEE0-7C9E659CA2B0} Options: 2 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 7  Current: {D1720CEC-81FF-4460-A5CB-86BC9B5AB7F5} Options: 1 Next Link: ['{D837ECDC-897D-4A91-89E3-F6222FE12F06}']
Question Text: Connect 8  Current: {D837ECDC-897D-4A91-89E3-F6222FE12F06} Options: 1 Next Link: []
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
['{EA65B6DA-90FE-412A-AF1F-83F994CCB5FA}']
ect....

I'm honestly very very lost as to how to go about this, I would greatly appreciate any help anyone could give老实说,我对如何解决这个问题感到非常迷茫,我将不胜感激任何人都可以提供的帮助

I've worked it out now.我现在已经解决了。 I had the for in the wrong place.我在错误的地方使用了 for。

Its still not perfect as I can't make it work where there are 2 links but for now this gets me past the forever loop to be able to work out the rest它仍然不完美,因为我无法让它在有 2 个链接的地方工作,但现在这让我超越了永远循环,能够解决其余的问题

class LinkedList():
def __init__(self, nodes=None):
    self.head = None
    self.reg_num = 0
    if nodes is not None:
        box_id = nodes[0].box_id
        while box_id != []:
            node_range = len(nodes)
            for node in range(node_range):
                if box_id == nodes[node].box_id:
                    name = nodes[node].name
                    print(f'name {name}')
                    next_id = nodes[node].next
                    if next_id != []:
                        next_id = next_id[0] #This needs to be something more clever
                    box_id = next_id

and prints和版画

name Connect 1
name Connect 2
name Connect 3
name Connect 4
name Connect 5
name Connect 7
name Connect 8

=================================================== ================================================== =

Edit: worked out how to do whole thing now编辑:现在弄清楚如何做整件事

start_id = nodes[0].box_id
plz_Work = LinkedList(nodes, start_id)

class班级

class LinkedList():
def __init__(self, nodes, start_id):
    if nodes is not None:
        box_id = start_id
        while box_id != []:
            node_range = len(nodes)
            for node in range(node_range):
                if box_id == nodes[node].box_id:
                    name = nodes[node].name
                    print(f'name: {name}')
                    next_id = nodes[node].next
                    if next_id != []:
                        next_range = len(next_id)
                        if next_range < 2:
                            next_id = next_id[0]
                        else:
                            for next_num in range(next_range):
                                LinkedList(nodes, next_id[next_num])
                            next_id = []
                    box_id = next_id

Print打印

name: Connect 1
name: Connect 2
name: Connect 3
name: Connect 4
name: Connect 5
name: Connect 7
name: Connect 8
name: Connect 6
name: Connect 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM