Форум сайта python.su
Здравствуйте
Нужна ваша помощь!
Задание: дан список простых чисел. Нужно сделать функцию, которая определит наименее часто встречающееся число. Нельзя использовать продвинутые методы, код должен быть самый базовый как у новичков.
Буду очень признателен любой помощи!!
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.
Отредактировано Valera_pyt (Дек. 3, 2017 20:14:23)
Офлайн
def min_count(x): res = x[0] for i in set(x): if x.count(res)>x.count(i): res = i return res
Офлайн
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 >>>
Отредактировано py.user.next (Дек. 4, 2017 03:34:47)
Офлайн