Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,13 @@ def mode(data):
if len(table) == 1:
return table[0][0]
elif table:
raise StatisticsError(
'no unique mode; found %d equally common values' % len(table)
)
_data = list(value for value, frequency in table)
if _data == list(data):
raise StatisticsError(
'no unique mode; found %d equally common values' % len(table)
)
else:
return _data
else:
raise StatisticsError('no mode for empty data')

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1788,8 +1788,8 @@ def test_bimodal_data(self):
# Test mode with bimodal data.
data = [1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 9]
assert data.count(2) == data.count(6) == 4
# Check for an exception.
self.assertRaises(statistics.StatisticsError, self.func, data)
# Check for a list of modes.
self.assertEqual(self.func(data), [2, 6])

def test_unique_data_failure(self):
# Test mode exception when data points are all unique.
Expand Down