• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Finding MODE

Q&A Forum › Category: ESP32 › Finding MODE
0 Vote Up Vote Down
Stephen Bradford asked 5 years ago

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.

4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Stephen.
For MicroPython, there is the ucollections module.
See here: https://docs.micropython.org/en/latest/library/ucollections.html
Regards,
Sara

0 Vote Up Vote Down
Stephen Bradford answered 5 years ago

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

0 Vote Up Vote Down
Stephen Bradford answered 5 years ago

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

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Great!
I’m glad you’ve found a way.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.