Make histogram in Python

Asked

Viewed 136 times

-1

Hello! I’m doing a histogram in Python, but in the line Histogram = Counter(decile(grids) for grade in grids) I find error with the Counter function. I already did the test using import Collections but I couldn’t. Following the lines developed so far:

grades = [83, 95 ,91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]
decile = lambda grade: grade / 10 * 10
histogram = collections.Counter(decile(grades) for grade in grades)

2 answers

1

Mauro, import is really necessary.

The error is no for inside the Counter, you sent the list and not the list item:

import collections

grades = [83, 95 ,91, 87, 70, 0, 85, 82, 100, 67, 73, 77, 0]
decile = lambda grade: grade / 10 * 10
histogram = collections.Counter(decile(grade) for grade in grades)
  • Thank you Daniel! In this case, what would be the appropriate structure?

-3

Place...

from collections import Counter
  • How he used collections.Counter, the correct would only be import collections. Or failed to supplement your answer by asking how it would be when doing the import as suggested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.