문제 설명
python, 출력으로 코딩하는 동안 pycharm에서 이 메시지를 받았습니다. :TypeError: can't convert type 'list' to numerator/denominator (python , I got this message in pycharm while coding as output :TypeError: can't convert type 'list' to numerator/denominator)
목록에 있는 목록의 평균을 평가하려고 하는데 다음은 코드입니다.
from statistics import mean
lst = [[1,2,3],
[4,5,6],
[7,8,9]]
total_avg = mean(lst)
print("The average is ", round(total_avg ,2))
</code></pre>
참조 솔루션
방법 1:
You are getting the error because you have not unpacked the list of lists. You have to unpack it first.
In your code the input for the mean()
function would be [1,2,3],[4,5,6],[7,8,9]
, instead of [1,2,3,4,5,6,7,8,9]
.
import statistics
lst = [[1,2,3],
[4,5,6],
[7,8,9]]
lst = [item for sublist in lst for item in sublist]
total_avg = statistics.mean(lst)
print("The average is ", round(total_avg ,2))
(by Radi3a、Bijin Abraham)
참조 문서