Hi I’m trying to return the most common number in a list using Micropython and an ESP32.
The list is a series of 10 readings generated from a sensor.
I have googled and found that module collections.Counter can be used, but that only seems to work with python not Micropython. Maybe I could create my own module for this. Any help would be appreciated. Thanks Steve.
Hi Stephen.
For MicroPython, there is the ucollections module.
See here: https://docs.micropython.org/en/latest/library/ucollections.html
Regards,
Sara
Thanks Sara, I have looked at ucollections but this does not have class “Counter” that is a container that keeps track of how many times equivalent values are added. ucollections is very limited. See https://pymotw.com/3/collections/counter.html
Hi Sara, Found another way.
def most_frequent(List):
return max(set(List), key = List.count)
List = [2982, 2191, 2976, 2982, 1987, 3098]
print(most_frequent(List))
2982