Home > OS >  I'm trying to round off the elements in a list (type list doesn't define __round__ method)
I'm trying to round off the elements in a list (type list doesn't define __round__ method)

Time:12-20

I'm trying to round off each item in the list but I always receive an error when executing my program.

Here is my code:

temp_answer = [[sum(q*w for q,w in zip(row,col)) for col in zip (*matrix_x)] for row in matrix_a]

final_answer = [[temp_answer[O][P]   matrix_b[O][P] for P in range(len(temp_answer[0]))] for O in range(len(temp_answer))]

roundtotenths = [[round(chicks,1) for chicks in final_answer]]

But I always receive an error that: type list doesn't define round method

CodePudding user response:

It looks like the last comprehension should be something like this: roundtotenths = [[round(chick,1) for chick in chicks] for chicks in final_answer]

CodePudding user response:

It appears final_answer is a list of lists:

final_answer = [[temp_answer[O][P]   matrix_b[O][P] for P in range(len(temp_answer[0]))] 
                for O in range(len(temp_answer))]

So naturally, you get that error. To correct, I suggest:

roundtotenths = [[round(chick,1) for chick in chicks] for chicks in final_answer]
  • Related