【发布时间】:2019-04-24 21:34:14
【问题描述】:
需要创建一个在不更改原始列表的情况下删除重复项的函数。 而在第二个中,它需要更改原始列表并且什么也不返回。 这里的问题是第二个函数只适用于一个重复而不适用于 2 个不同的数字(它只是删除一个数字(它不适用于 [2,3,4,5,3,4] 但适用于 [1,2, 3,3]
def drop_duplicates(lst):
s = []
# Write the rest of the code for question 3a below here.
for i in lst:
if i not in s:
s.append(i)
return s
lst = [1, 2, 3, 2, 4, 2]
print drop_duplicates(lst)
print lst
def drop_duplicates_in_place(lst):
# Write the rest of the code for question 3b below here.
for i in range(len(lst) - 1):
for j in range ( i+1, len(lst) - 1):
if lst[i] == lst[j]:
lst.pop(j)
else:
continue
lst = [1, 2, 3, 2, 4, 2]
print lst
【问题讨论】:
标签: python python-2.7 duplicates