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= 列表(设置(新)) 类型错误:不可散列类型:“列表”
有任何想法吗?预先感谢您的所有帮助。