This is a very simple solution, possibly not the most efficient one (?) but a simple one.
data = get_data()
freqs, numbers = {}, {}
for i in data:
freqs[i] = freqs.get(i, 0) + 1
for n, c in freqs.items():
numbers[c] = numbers.get(c, []) + [n]
counts = list(numbers.keys())
res = (numbers[min(counts)], numbers[max(counts)])
Let's see in detail what we have in the script above, let's start with
the example data you gave,
In [1]: data = [13,12,11,13,14,13,7,11,13,14,12,14,14]
We are going to use two dictionaries,
In [2]: freqs, numbers = {}, {}
the first one is filled iterating on data
, its keys are the
individual numbers in data
and its values the frequency of each
number in data (see fotnote for freqs.get(…)
)
In [3]: for i in data: freqs[i] = freqs.get(i, 0) + 1
the second one is simply a reversal of the first one, the keys being
the frequencies and the values being lists of numbers with a given
frequency.
In [4]: for n, c in freqs.items(): numbers[c] = numbers.get(c, []) + [n]
In [5]: numbers
Out[5]: {1: [7], 2: [12, 11], 4: [13, 14]}
At this point we need a list with the keys of numbers
, i.e., the
occurrences
In [6]: counts = list(numbers.keys())
because we are interested in the minimum and maximum values of the
occurrences
In [7]: [numbers[min(counts)], numbers[max(counts)]]
Out[7]: [[7], [13, 14]]
Footnote: the .get(key, default_value)
method of a dictionary
returns a default value if the key is not present in the dictionary,
we used this feature with a 0
default value to sum the occurrences
of individual numbers, and with []
, the void list, to build a list
of all the numbers with given frequency.