繁体   English   中英

Python-创建一个函数以从未知数量的参数中获取唯一列表

[英]Python- creating a function to get a unique list from unknown number of arguments

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
>>> unique_list(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
>>> unique_list(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)
Traceback (most recent call last):
  File "", line 1, in 
    single_list(a,b)
  File "", line 6, in single_list
    new2= list(set(new))
TypeError: unhashable type: 'list'

有任何想法吗? 预先感谢您的所有帮助。

您可以使用一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']

您想要未知数量的参数:

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])

在您第二次尝试时,您几乎完全正确。

实际上,在那部分代码中,您将每个列表视为一个元素。 您可能想要考虑列表中的每个元素。 因此,您的代码可能是:

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']

您可以连接所有lists ,然后从该结果list创建一个set 此方法适用于函数外观类似于def unique_lists( *lists )任意数量的传入def unique_lists( *lists )

ret_list = []
for l in lists:
    ret_list = ret_list + l

return set( ret_list )
a= ['mary', 'james', 'john', 'john']
b= ['elsie', 'james', 'elsie', 'james']
def unique_list(a, b):
    a.extend(b)
    return list(set(a))

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM