简体   繁体   中英

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. 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. AmountsLinkedList is the class, while you want to iterate the amounts that relate to the tmp instance. 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).

  • One line further you have a reference to tmp.amount , but the tmp instance has no amount attribute. You'll want to reference the amount variable , ie without the 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. To make that happen, you need to add the following:

    Add this method to the AmountsLinkedList class:

     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`:

     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) :

     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. You can just drop it.

  • The MatrixLinkedList has an __iter__ method that references an unexisting values method. As you don't use this __iter__ method anywhere, you can just drop it.

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. You need to iterate over the list in tmp.amounts . So replace that with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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