为什么我的some_list没有定义????
我正在尝试在my_list中找到重复项,然后打印出没有重复项的所有内容
def list_of_dups(some_list):
new_list=[]
some_list=[]
some_list+=my_list
for i in some_list:
if i in new_list:
print "hi"
else:
new_list+=remember[i]
return new_list
dups = list_of_dups(some_list)
print dups
答案 0 :(得分:1)
我相信你的奇怪
new_list+=remember[i]
应该只是
new_list.append(i)
答案 1 :(得分:1)
根据您想要删除重复元素的方式,这可能会为您提供所需的输出:
def remove_dups(input_list):
output = []
for element in input_list:
if element in output:
print 'Duplicate Found: ' + str(element)
else:
output.append(element)
return output
test_list1 = [1, 1, 2, 3, 4, 5, 5, 6]
test_list2 = ['a', 'a', '2', 2, 'dsd']
print remove_dups(test_list1)
print remove_dups(test_list2)
# OUTPUT
# Duplicate Found: 1
# Duplicate Found: 5
# [1, 2, 3, 4, 5, 6]
# Duplicate Found: a
# ['a', '2', 2, 'dsd']
答案 2 :(得分:0)
好吧,如果您只是想完成最终目标,可以使用Python set
将列表转换为不重复的列表:
for e in set(some_list):
print e
但这不会维持some_list
的排序。
另一方面,如果您尝试将自己的功能作为练习实施......那么使用set
可能不是您想要的。 : - )
答案 3 :(得分:0)
def list_of_dups(some_list): ##some_list is passed from below
new_list=[]
some_list=[] ##some_list is cleared
some_list+=my_list ##where is my_list coming from?
for i in some_list: ###here some_list
if i in new_list:
print "hi"
else:
new_list+=remember[i]
return new_list
dups = list_of_dups(some_list)
print dups
这应该是
def list_of_dups(my_list): ##my_list is passed from below
new_list=[]
some_list=[] ##some_list is cleared
some_list+=my_list
for i in some_list: ###here some_list
if i in new_list:
print "hi"
else:
new_list+=remember[i]
return new_list
dups = list_of_dups(my_list)
print dups