【问题标题】:Python- creating a function to get a unique list from unknown number of argumentsPython-创建一个函数以从未知数量的参数中获取唯一列表
【发布时间】:2012-11-06 06:03:43
【问题描述】:

python 新手——谁能告诉我我做错了什么?

我需要编写一个函数,它接受未知数量的参数并返回一个唯一列表。 例如:

a= ['mary', 'james', 'john', 'john']
b= ['elsie', 'james', 'elsie', 'james']

unique_list(a,b)

['mary', 'james','john', 'elsie']

这是我在做一些研究后得到的一些代码,但输出不是我需要的:

def unique_list:(*something)
    result1= list(something)
    result = ' '.join(sum(result1, []))
    new= []
    for name in result:
            if name not in new:
                           new.append(name)
    return new
>>> 唯一列表(a,b) ['m','a','r','y','','j','e','s','o','h','n','l','i ']

这是我累了的另一个:

def unique_list(*something):
    result= list(something) 
    new=[]
    for name in result:
        if name not in new:
            new.append(name)
    return new
>>> 唯一列表(a,b) [['mary', 'james', 'john', 'john'], ['elsie', 'james', 'elsie', 'james']]

另一个,但我收到一条错误消息:

def single_list(*something):
    new=[]
    for name in something:
        if name not in new:
            new.append(name)
    new2= list(set(new))
    return new2
>>> single_list(a,b) 回溯(最近一次通话最后): 文件“”,第 1 行,在 单列表(a,b) 文件“”,第 6 行,在 single_list 中 new2= 列表(设置(新)) 类型错误:不可散列类型:“列表”

有什么想法吗?提前感谢您的所有帮助。

【问题讨论】:

    标签: python function unique


    【解决方案1】:

    您可以使用set

    def unique_list(a, b):
        return list(set(a + b))
    

    对于未知数量的参数,您可以将所有列表与reduce 一起添加:

    import operator
    def unique_list(*args):
        return list(set(reduce(operator.add, args)))
    

    这个输出:

    >>> a= ['mary', 'james', 'john', 'john']
    >>> b= ['elsie', 'james', 'elsie', 'james']
    >>> unique_list(a, b)
    ['james', 'john', 'mary', 'elsie']
    

    【讨论】:

    • 参数个数未知怎么办?
    【解决方案2】:

    在您第二次尝试时,您几乎成功了。

    实际上,在这部分代码中,您将每个列表视为一个元素。您可能想考虑列表中的每个元素。所以你的代码可能是:

    def unique_list(*something):
        result= list(something) 
        new=[]
        for names in result:
            for name in names:
                if name not in new:
                    new.append(name)
        return new
    

    结果是:

    ['mary', 'james', 'john', 'elsie']
    

    第三次尝试也是如此。请注意,在这种情况下,从您的列表中创建一个集合,然后从该集合中创建一个列表可能不会以与原始列表相同的顺序返回元素。

    根据你的需要,你也可以使用 itertools.chain()。函数是:

    import itertools
    
    def unique_list(*something):
        return list(set(itertools.chain(*something)))
    

    结果将是(记住集合不保持原始顺序):

    ['james', 'john', 'mary', 'elsie']
    

    【讨论】:

    • @ThiagoRRamos- 感谢您指出有关集合并展示另一种可能的解决方案
    【解决方案3】:

    您想要未知数量的参数:

    In [73]: import itertools
    
    In [74]: def func(*args):
        ...:     return set(itertools.chain(*args))
    
    In [75]: func([1,2,3,4],[3,4,5],[1,2])
    Out[75]: set([1, 2, 3, 4, 5])
    

    或者没有 itertools:

    In [77]: def func2(*args):
        ...:     setlist=[set(i) for i in args]
        ...:     return set.union(*setlist)
    
    In [78]: func2([1,2,3,4],[3,4,5],[1,2])
    Out[78]: set([1, 2, 3, 4, 5])
    

    【讨论】:

    • @user1667805——不客气。添加了另一个没有 itertools 的选项。
    【解决方案4】:

    您可以连接所有的lists,然后从生成的list 创建一个set。这适用于函数看起来像 def unique_lists( *lists ) 的任意数量的传入列表

    ret_list = []
    for l in lists:
        ret_list = ret_list + l
    
    return set( ret_list )
    

    【讨论】:

      【解决方案5】:
      a= ['mary', 'james', 'john', 'john']
      b= ['elsie', 'james', 'elsie', 'james']
      def unique_list(a, b):
          a.extend(b)
          return list(set(a))
      

      【讨论】:

      • 这不能解决问题 - 列表元素在结果列表中重复。
      【解决方案6】:

      您正在寻找的功能(我认为)是将两个列表组合在一起,然后再次将其制成一个列表。像这样:

      list(set(list(a+b)))
      

      给:['james', 'john', 'mary', 'elsie']

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 2016-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多