如何从 Python 列表中删除重复项

def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

1. 使用列表项作为键创建字典。这将自动删除任何重复项,因为词典不能有重复的键。
2. 然后,将字典转换回列表。

发布了186 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sinat_23971513/article/details/105286699