Home > Mobile >  How to fix this error when take data in Python
How to fix this error when take data in Python

Time:05-04

The problem here that when I put in user='U3' and debugging I see history1[:, 1][i1][j1] and transactions1[:, 0][m] have the same value. That is ['T500'] but the expression return false (also ['T600']) and when they are have value ['T1000'], they return true. And the last output is ['I1', 'I3', 'I2'] instead of ['I1', 'I3', 'I2', 'I3', 'I1', 'I3', 'I2']. Please give me some advice to fix this.

Thank for support

    def createMatrix(user, transactions1, history1):
        temp = []
        for i1 in range(len(history1[:, 0])):
            if history1[:, 0][i1] == user:
                for j1 in range(len(history1[:, 1][i1])):
                    for m in range(len(transactions1[:, 0])):
                        if history1[:, 1][i1][j1] == transactions1[:, 0][m]: #<------ Problem here
                            for item in transactions1[:, 1][m]: 
                                temp.append(item)
        
        listNew = dict(zip(*np.unique(temp, return_counts=True)))
        return temp

Argument: transactions1

[[array(['T100'], dtype='<U4') array(['I1', 'I2', 'I5'], dtype='<U2')]
 [array(['T200'], dtype='<U4') array(['I2', 'I4'], dtype='<U2')]
 [array(['T300'], dtype='<U4') array(['I2', 'I3'], dtype='<U2')]
 [array(['T400'], dtype='<U4') array(['I1', 'I2', 'I4'], dtype='<U2')]
 [array(['T500'], dtype='<U4') array(['I1', 'I3'], dtype='<U2')]
 [array(['T600'], dtype='<U4') array(['I2', 'I3'], dtype='<U2')]
 [array(['T700'], dtype='<U4') array(['I1', 'I3'], dtype='<U2')]
 [array(['T800'], dtype='<U4') array(['I1', 'I2', 'I3', 'I5'], dtype='<U2')]
 [array(['T900'], dtype='<U4') array(['I1', 'I2', 'I3'], dtype='<U2')]
 [array(['T1000'], dtype='<U5') array(['I1', 'I3', 'I2'], dtype='<U2')]]

Argument: history1

[[array(['U1'], dtype='<U2') array(['T100', 'T400'], dtype='<U4')]
 [array(['U2'], dtype='<U2') array(['T200', 'T300', 'T700'], dtype='<U4')]
 [array(['U3'], dtype='<U2') array(['T500 ', 'T600 ', 'T1000'], dtype='<U5')]
 [array(['U4'], dtype='<U2') array(['T800'], dtype='<U4')]
 [array(['U5'], dtype='<U2') array(['T900'], dtype='<U4')]]

CodePudding user response:

Your history1 argument has spaces after T500 and T600.

 [array(['U3'], dtype='<U2') array(['T500 ', 'T600 ', 'T1000'], dtype='<U5')]

Your transaction1 argument does not.

[array(['T500'], dtype='<U4') array(['I1', 'I3'], dtype='<U2')]
[array(['T600'], dtype='<U4') array(['I2', 'I3'], dtype='<U2')]

'T500 ' is not equal to 'T500'

Either tidy your data before creating history1 and transactions1 arguments to remove the trailing space, or add a .strip() on the line with the problem.

if history1[:, 1][i1][j1].strip() == transactions1[:, 0][m].strip(): #<------ Problem here
  • Related