Найти - Пользователи
Полная версия: менее повторяемое число в списке
Начало » Python для новичков » менее повторяемое число в списке
1
Valera_pyt
Здравствуйте

Нужна ваша помощь!

Задание: дан список простых чисел. Нужно сделать функцию, которая определит наименее часто встречающееся число. Нельзя использовать продвинутые методы, код должен быть самый базовый как у новичков.
Буду очень признателен любой помощи!!


Given a list of integers, where each integer may appear multiple times, we want to identify the least common integer in the list. If different integers have the same lowest frequency then the result is the one occurring latest in the list. For example, the least common integer in 3, 4, 4, 3 is 3.
marvellik
 def min_count(x):
    res = x[0]
    for i in set(x):
        if x.count(res)>x.count(i):
            res = i
    return res
py.user.next
Valera_pyt
Given a list of integers, where each integer may appear multiple times, we want to identify the least common integer in the list. If different integers have the same lowest frequency then the result is the one occurring latest in the list. For example, the least common integer in 3, 4, 4, 3 is 3.
  
>>> def f(seq):
...     dct = {}
...     for i, e in enumerate(seq):
...         dct[e] = dct.get(e, 0) + 1
...     tmp = None
...     for k, v in dct.items():
...         if tmp is None or v <= tmp[1]:
...             tmp = k, v
...     out = tmp[0]
...     return out
... 
>>> f([1, 1, 2, 2, 3, 3, 4, 4])
4
>>> f([1, 1, 2, 2, 3, 3, 4, 4, 4])
3
>>> f([1, 1, 2, 2, 2, 3, 3, 4, 4, 4])
3
>>>
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB