在Python 3.0中从列表更改为参数

在Python 3.0中从列表更改为参数,python,counter,Python,Counter,我最近构建了一段python代码,它可以在列表中找到最不常见的重复数!这是我的密码 从收款进口柜台 def least_common(): from collections import Counter List = [1,1,1,0,0,3,3,2] CountList = Counter(List) Mincount = min(CountList.values()) least_common = next(n for n in reversed(List) if CountLis

我最近构建了一段python代码,它可以在列表中找到最不常见的重复数!这是我的密码

从收款进口柜台

def least_common():

from collections import Counter

List = [1,1,1,0,0,3,3,2]

CountList = Counter(List)

Mincount = min(CountList.values())

least_common = next(n for n in reversed(List) if CountList[n] == Mincount)

print (least_common)
最不常见的

然而,正如您可以清楚地看到的,这使用了一个列表来调用将要比较的数字。 我现在正试图让它执行相同的任务,但是我希望它使用整数参数,而不是使用内置列表

比如说

def the_least_common(integers)

--------code with argument which will find lowest repeated number---------

   print the_least_common([1,1,1,0,0,3,3,2])
最不常见的是2

我已经创建的任何代码是否可以重用,以满足我现在需要创建的需求?如果这是一个愚蠢的问题,或者我有点被卡住了,我会道歉


任何建议都将不胜感激

由于您使用的是
计数器
,因此有一个内置方法--返回元素及其计数的排序列表,从最常见的第一个开始。您可以查询此列表的最后一个元素

In [418]: Counter([1,1,1,0,0,3,3,2]).most_common()[-1]
Out[418]: (2, 1)
您的函数如下所示:

def least_common(data):
    return Counter(data).most_common()[-1][0]

如果您的数据可以有多个具有相同最小计数的整数,并且您的函数需要返回其中的每一个整数,那么您可以在最常见的
上迭代:

def least_common(data):
    c = Counter(data).most_common()[::-1]
    yield c[0][0]

    for x, y in c[1:]:
        if x != c[0][1]:
            break
        yield y

请格式化您的代码。很抱歉@Shadow code now Formatted我知道OP没有指定这一点,但严格来说,可能有多个最不常见的元素。@Os9008那么这里有什么问题?@cᴏʟᴅsᴘᴇᴇᴅ 很抱歉,我没有完成我的评论。我的def需要是def最小公共数(整数),并调用print最小公共数([1,1,1,0,0,3,3,2])来指定程序。谢谢你的提示anyway@Os9008除了函数名中缺少的“the”之外,我的答案与您的“specification”有什么不同?别介意,伙计,我已经破解了它!我现在感觉好极了!我是编程新手,我很高兴我找到了一个解决方案!无论如何谢谢你的帮助:)@cᴏʟᴅsᴘᴇᴇᴅ