簡體   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))

我正在嘗試做一種鏈表的鏈表,我認為我沒問題,但現在我想通過它的索引獲得一個鏈表,但該鏈表內部還有其他鏈表,但是以對象的方式,所以我會喜歡迭代那個對象。

我試圖使用__iterate__但它不起作用,也許是因為我使用它的方式不好。 使用它我在控制台中得到下一個:

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

我想要的是將該對象轉換為值,以便在控制台中看起來像這樣:

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

我希望有人可以幫助我,在此先感謝。

一些問題:

  • for amount in AmountsLinkedList:不正確。 AmountsLinkedList是類,而您想要迭代與tmp實例相關的數量。 所以這可能是:

     for amount in tmp.amounts:

    ...但您必須使用適當的__iter__實現使tmp.amounts可迭代(請參見下文)。

  • 在另一行中,您引用了tmp.amount ,但tmp實例沒有amount屬性。 您將需要引用amount變量,即沒有tmp. 字首。

  • 下面的一行,你想用tmp.amounts創建一個字符串,但tmp.amounts不會被表示為一個具有該鏈表中值的可讀字符串。 為此,您需要添加以下內容:

    將此方法添加到AmountsLinkedList類:

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

    並通過將此方法添加到Information類,使Information實例可表示為它的數量:

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

    最后,當您使用.format()構建字符串時,使用list(tmp.amounts)將鏈接列表轉換為標准列表:

     return 'm: {}, n: {}, name: {}, amounts: {}'.format( tmp.m, tmp.n, tmp.name, list(tmp.amounts) )
  • 您在Information類上定義的__iter__方法不是很有用:它不會產生任何東西。 你可以放下它。

  • MatrixLinkedList有一個__iter__方法,它引用了一個不存在的values方法。 由於您不在任何地方使用此__iter__方法,因此您可以將其刪除。

因此,所有這些更改都會導致此代碼(可以進一步改進):

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

是不正確的。 AmountsLinkedList不是金額列表,而是類。 您需要遍歷tmp.amounts的列表。 因此,將其替換為:

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