简体   繁体   English

如何打印或迭代链接列表的链接列表的对象?

[英]How to print or iterate over an object of a linked list of linked lists?

class Information:#This is a node class
    def __init__(self, x, y, amount):
        self.x = x
        self.y = y
        self.amount = amount
        self.next = None
    
    def __iter__(self):
        return self.amount
    
    def getValue(self):
        return self.amount

class AmountsLinkedList:#Its node is Information class
    def __init__(self):
        self.start = None
    
    def insertInfo(self, x, y, amount):
        new = Information(x, y, amount)
        if self.start is None:
            self.start = new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new

class Matrix:#This is a node class
    def __init__(self,counter, m, n, name, amounts):
        self.m = m
        self.n = n
        self.name = name
        self.amounts = amounts
        self.counterr = counter
        self.next = None
        self.prev = None

class MatrixLinkedList:#Its node is Matrix class
    def __init__(self):
        self.start = None

    def insertMatrix(self,counterr, m, n, name, amounts):
        new = Matrix(counterr, m, n, name, amounts)
        if self.start is None:
            self.start = new
            return new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new
            new.prev = tmp
            return new
        return None
    
    def __iter__(self):
        return self.amounts.values().__iter__()

    def getMatrixByIndex(self, counterr):
        tmp = self.start
        while tmp is not None:
            if tmp.counterr == counterr:
                for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
                    print('->. '+str(tmp.amount.getValue()))
                return 'm: {}, n: {}, name: {}, amounts: {}'.format(tmp.m, tmp.n, tmp.name, tmp.amounts)
                
            
            tmp = tmp.next
        return None
    
    def length(self):
        cur = self.start
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

if __name__ == '__main__':
    ld=AmountsLinkedList()
    
    ld.insertInfo(1, 20, 'David')
    ld.insertInfo(2, 10, 'John')
    ld.insertInfo(4, 78, 'Sam')
    ld.insertInfo(12, 12, 'Peter')
    
    ld2=AmountsLinkedList()
    ld2.insertInfo(1, 20, 'David')
    ld2.insertInfo(2, 10, 'John')
    ld2.insertInfo(4, 78, 'Sam')
    
    lm = MatrixLinkedList()
    
    #Sending the two linked lists (ld and ld2) in lm linked lists
    lm.insertMatrix(1,3, 3, 'matrix_1', ld)
    lm.insertMatrix(2,4, 3, 'matrix_2', ld2)
    #Above what I'm passing in first parameter is a kind of index to then compare it in "getMatrizByIndex" method
    print('*****Matrix class has',lm.length(),'linked lists inside*****')
    print(lm.getMatrixByIndex(2))

I am trying to do a kind of linked list of linked list, and I thought I was ok but now I wanna get a linked list by its index, but that linked list has other linked list inside but in an object way, so I would like to iterate over that object.我正在尝试做一种链表的链表,我认为我没问题,但现在我想通过它的索引获得一个链表,但该链表内部还有其他链表,但是以对象的方式,所以我会喜欢迭代那个对象。

I was trying to use __iterate__ but it doesn´t work, maybe because I am using it in a bad way.我试图使用__iterate__但它不起作用,也许是因为我使用它的方式不好。 Using that I get the next in console:使用它我在控制台中得到下一个:

hctr@DESKTOP-0VFHRDP MINGW64 ~/Documents/list_of_lists (master)
$ C:/Users/hctr/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/hctr/Documents/list_of_lists/testing.py
*****Matrix class has 2 linked lists inside*****
Traceback (most recent call last):
  File "c:/Users/hctr/Documents/list_of_lists/testing.py", line 99, in <module>
    print(lm.getMatrizByIndex(2))
  File "c:/Users/hctr/Documents/list_of_lists/testing.py", line 63, in getMatrizByIndex
    for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
TypeError: 'type' object is not iterable

And what I want is convert that object into the values so in console would look like this:我想要的是将该对象转换为值,以便在控制台中看起来像这样:

hctr@DESKTOP-0VFHRDP MINGW64 ~/Documents/list_of_lists (master)
$ C:/Users/hctr/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/hctr/Documents/list_of_lists/testing.py
*****Matrix class has 2 linked lists inside*****
m: 4, n: 3, name: matrix_2, amounts: <__main__.AmountsLinkedList object at 0x00CF5028>
David
John
Sam

I hope someone can help me, thanks in advance.我希望有人可以帮助我,在此先感谢。

Some issues:一些问题:

  • for amount in AmountsLinkedList: is not right. for amount in AmountsLinkedList:不正确。 AmountsLinkedList is the class, while you want to iterate the amounts that relate to the tmp instance. AmountsLinkedList是类,而您想要迭代与tmp实例相关的数量。 So this could be:所以这可能是:

     for amount in tmp.amounts:

    ...but you'll have to make tmp.amounts iterable with a proper __iter__ implementation (see further down). ...但您必须使用适当的__iter__实现使tmp.amounts可迭代(请参见下文)。

  • One line further you have a reference to tmp.amount , but the tmp instance has no amount attribute.在另一行中,您引用了tmp.amount ,但tmp实例没有amount属性。 You'll want to reference the amount variable , ie without the tmp.您将需要引用amount变量,即没有tmp. prefix.字首。

  • One line below that, you want to create a string with tmp.amounts , but tmp.amounts will not be represented as a nicely readable string with the values in that linked list.下面的一行,你想用tmp.amounts创建一个字符串,但tmp.amounts不会被表示为一个具有该链表中值的可读字符串。 To make that happen, you need to add the following:为此,您需要添加以下内容:

    Add this method to the AmountsLinkedList class:将此方法添加到AmountsLinkedList类:

     def __iter__(self): node = self.start while node: yield node node = node.next

    And make an Information instance representable as its amount by adding this method to the Information class`:并通过将此方法添加到Information类,使Information实例可表示为它的数量:

     def __repr__(self): return str(self.amount)

    Finally, when you build the string with .format() , cast the linked list to a standard list with list(tmp.amounts) :最后,当您使用.format()构建字符串时,使用list(tmp.amounts)将链接列表转换为标准列表:

     return 'm: {}, n: {}, name: {}, amounts: {}'.format( tmp.m, tmp.n, tmp.name, list(tmp.amounts) )
  • The __iter__ method you have defined on the Information class is not very useful: it doesn't yield anything.您在Information类上定义的__iter__方法不是很有用:它不会产生任何东西。 You can just drop it.你可以放下它。

  • The MatrixLinkedList has an __iter__ method that references an unexisting values method. MatrixLinkedList有一个__iter__方法,它引用了一个不存在的values方法。 As you don't use this __iter__ method anywhere, you can just drop it.由于您不在任何地方使用此__iter__方法,因此您可以将其删除。

So all these changes lead to this code (which could be further improved):因此,所有这些更改都会导致此代码(可以进一步改进):

class Information:#This is a node class
    def __init__(self, x, y, amount):
        self.x = x
        self.y = y
        self.amount = amount
        self.next = None
    
    def getValue(self):
        return self.amount

    def __repr__(self):
        return str(self.amount)

class AmountsLinkedList:#Its node is Information class
    def __init__(self):
        self.start = None
    
    def insertInfo(self, x, y, amount):
        new = Information(x, y, amount)
        if self.start is None:
            self.start = new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new

    def __iter__(self):
        node = self.start
        while node:
            yield node
            node = node.next
    
class Matrix:#This is a node class
    def __init__(self,counter, m, n, name, amounts):
        self.m = m
        self.n = n
        self.name = name
        self.amounts = amounts
        self.counterr = counter
        self.next = None
        self.prev = None

class MatrixLinkedList:#Its node is Matrix class
    def __init__(self):
        self.start = None

    def insertMatrix(self,counterr, m, n, name, amounts):
        new = Matrix(counterr, m, n, name, amounts)
        if self.start is None:
            self.start = new
            return new
        else:
            tmp = self.start
            while tmp.next is not None:
                tmp = tmp.next
            tmp.next = new
            new.prev = tmp
            return new
        return None
    
    def getMatrixByIndex(self, counterr):
        tmp = self.start
        while tmp is not None:
            if tmp.counterr == counterr:
                #for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
                for amount in tmp.amounts:
                    print('->. '+str(amount.getValue())) #just amount
                return 'm: {}, n: {}, name: {}, amounts: {}'.format(tmp.m, tmp.n, tmp.name, list(tmp.amounts))
                
            
            tmp = tmp.next
        return None
    
    def length(self):
        cur = self.start
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

if __name__ == '__main__':
    ld=AmountsLinkedList()
    
    ld.insertInfo(1, 20, 'David')
    ld.insertInfo(2, 10, 'John')
    ld.insertInfo(4, 78, 'Sam')
    ld.insertInfo(12, 12, 'Peter')
    
    ld2=AmountsLinkedList()
    ld2.insertInfo(1, 20, 'David')
    ld2.insertInfo(2, 10, 'John')
    ld2.insertInfo(4, 78, 'Sam')
    
    lm = MatrixLinkedList()
    
    #Sending the two linked lists (ld and ld2) in lm linked lists
    lm.insertMatrix(1,3, 3, 'matrix_1', ld)
    lm.insertMatrix(2,4, 3, 'matrix_2', ld2)
    #Above what I'm passing in first parameter is a kind of index to then compare it in "getMatrizByIndex" method
    print('*****Matrix class has',lm.length(),'linked lists inside*****')
    print(lm.getMatrixByIndex(2))
for amount in AmountsLinkedList:#Here I am trying to access to the data in the object of the linked list
    print('->. '+str(tmp.amount.getValue()))

is not correct.是不正确的。 AmountsLinkedList is not the amounts list, it's the class. AmountsLinkedList不是金额列表,而是类。 You need to iterate over the list in tmp.amounts .您需要遍历tmp.amounts的列表。 So replace that with:因此,将其替换为:

amount = tmp.amounts.start
while amount is not None:
    print('->. ' + str(amount.getValue())
    amount = amount.next

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

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