几乎我需要编写一个程序来检查列表是否有任何重复项,如果有,它会删除它们并返回一个新列表,其中包含未复制/删除的项目.这就是我所拥有的,但说实话,我不知道该怎么做.
def remove_duplicates():
t = ['a', 'b', 'c', 'd']
t2 = ['a', 'c', 'd']
for t in t2:
t.append(t.remove())
return t
Run Code Online (Sandbox Code Playgroud) 是否有内置功能可以从Python中的列表中删除重复项,同时保留顺序?我知道我可以使用一个集来删除重复项,但这会破坏原始顺序.我也知道我可以像这样滚动自己:
def uniq(input):
output = []
for x in input:
if x not in output:
output.append(x)
return output
Run Code Online (Sandbox Code Playgroud)
但是如果可能的话,我想利用内置或更多的Pythonic习语.