1

I am working on a problem that returns maximum occurring elements in a list.

I am able to achieve this using mode in statistics. But when there are more than one element that repeats most, the mode function is throwing StatisticsError. How do I get number of mode values in the list.

I have a list

[1,1,1,2,2,2,3,3,4,4]

1's - 3

2's - 3

3's - 2

4's - 2

Here, most repeated elements are 1 and 2. Both are repeated thrice which is maximum occurrences for any number in the list.

How do I get 1 and 2s in a separate list. Is there any function to do that? Or Am I looking at it the wrong way?

  • 4
    Look at collections.Counter – Dani Mesejo Jan 01 '19 at 16:13
  • Is the list sorted? – user2390182 Jan 01 '19 at 16:13
  • 1
    heads up, dont use the accepted answer in the dupe, use the collections Counter – Paritosh Singh Jan 01 '19 at 16:24
  • I've used Counter. But the `most_common()` is not giving what I wanted. Instead it is giving elements in an order. – user10849817 Jan 01 '19 at 16:43
  • I think we don't understand what you mean *How do I get 1s and 2s in a separate list?* Do you mean *only* the 1s and 2s (but not the 3s and 4s, and if not, why not?) and do you want 1s in one list and 2s in another, or what? So please resubmit your question to show the output you want and showing how that is different from what a `Counter` gives you. There's no point in editing the question now because it has been closed as a duplicate, even if that was because we didn't understand what you were trying to do. – BoarGules Jan 01 '19 at 17:23
  • Updated the question – user10849817 Jan 01 '19 at 18:04
  • I see you updated the question. But SO advised you (in the yellow panel) not to do that, and I repeated that advice in my comment, with the reason why: after a question has been marked as a duplicate, nobody can answer it: the existing answer is considered sufficient. So, please post your question again. If you will take my advice, do not say *How do I get 1 and 2s in a separate list?* because that is open to interpretation. Say rather you want to see `[1,1,1,2,2,2]`. And if the input is `[1,2,1,2,1,2,3,3,4,4]` do you want to see `[1,2,1,2,1,2]` or is `[1,1,1,2,2,2]` okay in that case also? – BoarGules Jan 02 '19 at 12:17
  • any of the above lists is fine as I can get a Counter object from that again which eases the work. – user10849817 Jan 02 '19 at 19:32

0 Answers0