简体   繁体   中英

Using a while loop to iterate class members in python 3

I have a set of code that takes a list full of simple objects and iterates through the objects and compares them to each other. The list is 30 objects long and the objects have 8 class members.

def createSijkTest(data):
    Sijk = np.empty(getDimensions(data))
    x = 0
        for item0 in data:
            y = 0
            for item1 in data:
                z = 0
                while z < 8:
                    member0 = item0.__dict__.items()
                    member1 = item1.__dict__.items()
                    if member0[z] == member1[z]:
                        Sijk[x,y,z] = 1
                    else:
                        Sijk[x,y,z] = 0
                z += 1
            y += 1
        x += 1

The output should be a numpy array of 30x30x8 dimensions. I get an error: TypeError: 'dict_items' object does not support indexing. I understand why I get the error, but I don't know how to fix it.

The code for the objects is:

class row:
    def __init__(self, L):
        self.dDate = []
        self.name = []
        self.dType = []
        self.city = []
        self.state = []
        self.rCommittee = []
        self.employer = []
        self.amount = []
        self.dDate.append(L[0])
        self.dType.append(L[1])
        self.name.append(L[2])
        self.city.append(L[3])
        self.state.append(L[4])
        self.rCommittee.append(L[5])
        self.employer.append(L[6])
        self.amount.append(float(L[7]))

您可以尝试的最直接的方法是将其转换为列表:

list(item0.__dict__.items())

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