1

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= 列表(设置(新))
类型错误:不可散列类型:“列表”

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

4

6 回答 6

5

您可以使用set

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

对于未知数量的参数,您可以将所有列表与 a 一起添加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']
于 2012-11-06T06:07:02.117 回答
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])
于 2012-11-06T06:17:15.577 回答
3

在你第二次尝试时,你几乎做对了。

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

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']
于 2012-11-06T06:33:00.313 回答
1

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

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

return set( ret_list )
于 2012-11-06T06:07:38.187 回答
1
a= ['mary', 'james', 'john', 'john']
b= ['elsie', 'james', 'elsie', 'james']
def unique_list(a, b):
    a.extend(b)
    return list(set(a))
于 2012-11-06T08:45:54.490 回答
0

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

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

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

于 2012-11-06T06:07:52.663 回答