python - How to get the most represented object from an array -
I have an array with some objects, and many objects are the same eg: fruit = [apple, orange, apple, Banana, banana, orange, apple, apple]
What is the most effective way to get the most represented object from this array? In this case it will be "apple", but how will you go out and calculate in an efficient way?
Do not recapture the wheel In Python 2.7+ you can use:
< Pre-> import collection fruit = ['apple', 'orange', 'apple', 'banana', 'banana', 'orange', 'apples' ('apple', 4)]
< / Pre> If you are using an older version of Python, you can download the counter
.
Although it is good to know how to implement yourself in this way, it is also a good idea to use the counter to use it because it is part of the standard library (or Happening).
Comments
Post a Comment