【问题标题】:Looping through a linked list containing arrays in python循环遍历包含python中数组的链表
【发布时间】:2018-11-14 14:08:53
【问题描述】:

我正在努力研究如何使用循环来遍历一个列表,该列表包含 4 个数组值,即“a_value”、“b_value”、“c_value”、“d_value”。此列表中的每个数组都有 23 个元素。我正在寻找的结果是通过添加每个数组的所有 23 个元素的列表来运行循环。例如。 “a_value”的总和,然后是“b_value”的总和,依此类推。这就是我到目前为止所拥有的。 谢谢各位。

class node():
    def __init__(self, dataval = None):
        self.dataval = dataval
        self.nextval = None

class linked_list():
    def __init__(self):
        self.headval = None

    def listprint(self):
        printval = self.headval
        while printval != None:
            print (printval.dataval)
            printval = printval.nextval        


arraylist = linked_list()
arraylist.headval = node(a_value) # each '""_value' has 23 integer values 
in them
e2 = node(b_value)
e3 = node(c_value)
e4 = node(d_value) 

arraylist.headval.nextval = e2
e2.nextval = e3
e3.nextval = e4

arraylist.listprint()

i = 1
x = 1
total_a = 0
total_b = 0
total_c = 0
total_d = 0

for n in arraylist:
    for z in range(len(n)-1)
        value_a = int(a_value[i])
        total_a+=value_a
        i+= 1
    x+=1        

【问题讨论】:

  • 您的输入和预期输出是什么?
  • 对您有帮助吗?

标签: python arrays linked-list nested-loops


【解决方案1】:

你可以试试这样的:

    a_value = [1,2,3]
    b_value = [4,5,6]
    c_value = [7,8,9]
    d_value = [10,11,12]

    class node():
        def __init__(self, dataval = None):
            self.dataval = dataval
            self.nextval = None


    class linked_list():
        def __init__(self):
            self.headval = None

        def listprint(self):
            printval = self.headval
            while printval != None:
                print (printval.dataval)
                printval = printval.nextval


    arraylist = linked_list()
    arraylist.headval = node(a_value)
    e2 = node(b_value)
    e3 = node(c_value)
    e4 = node(d_value)

    arraylist.headval.nextval = e2
    e2.nextval = e3
    e3.nextval = e4

    node = arraylist.headval     # fetch head node  
    nextval = node.nextval         # fetch next to node
    list = []
    list.append(node.dataval)       # enter all head list

    while nextval:              # this loop will break as nextval is none
        node = node.nextval     # node now becomes next and repeat
            if hasattr(node, 'nextval'):
                nextval = node.nextval
            else:
                nextval = None
        list.append(node.dataval)

    sum = 0

    for x in list:          # sum in the end or break into 23 for all separate
        for y in x:
            sum+=y

    print(list, sum)

    #OUTPUT [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 78

工作正常:)

【讨论】:

  • (nextval = node.nextval) 出现错误“'Nonetype' 对象没有属性 'nextval'”
  • 在获取值之前添加一个检查。
  • 你会使用什么样的支票?
  • 太棒了!谢谢你,伙计!我只是好奇的最后一件事,有没有办法得到它,所以它会打印“[[6],[15],[24],[33]]”而不是谢谢:)
  • 你必须解决这个问题:D 学习并接受答案,如果它对你有帮助:p
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多