Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中删除列表中的重复项_Python_List - Fatal编程技术网

在python中删除列表中的重复项

在python中删除列表中的重复项,python,list,Python,List,假设我有一份 ['a','man', 'and', 'a', 'woman'] 如何删除重复的“a”,使其仅为: ['a','man', 'and', 'woman'] 维持秩序: >>> from collections import OrderedDict >>> L = ['a','man', 'and', 'a', 'woman'] >>> list(OrderedDict.fromkeys(L)) ['a', 'man', 'a

假设我有一份

['a','man', 'and', 'a', 'woman']
如何删除重复的“a”,使其仅为:

['a','man', 'and', 'woman']
维持秩序:

>>> from collections import OrderedDict
>>> L = ['a','man', 'and', 'a', 'woman']
>>> list(OrderedDict.fromkeys(L))
['a', 'man', 'and', 'woman']
维持秩序:

>>> from collections import OrderedDict
>>> L = ['a','man', 'and', 'a', 'woman']
>>> list(OrderedDict.fromkeys(L))
['a', 'man', 'and', 'woman']

如果订单不重要,则您可以执行以下操作:

d = ['a', 'man', 'and', 'a', 'woman']
list(set(d))

如果订单不重要,则您可以执行以下操作:

d = ['a', 'man', 'and', 'a', 'woman']
list(set(d))

如果顺序很重要,类似于@jamylak的建议是使用
OrderedSet
recipe

list(OrderedSet(L))

如果顺序很重要,类似于@jamylak的建议是使用
OrderedSet
recipe

list(OrderedSet(L))

完成此操作后,不妨将其保留为
集合
。完成此操作后,不妨将其保留为
集合
。我注意到您问了很多问题。你应该接受每一个被证明有帮助的问题的答案。有人问了这个问题,然后删除了他们的评论,顺序重要吗?我注意到你问了很多问题。您应该接受每个问题中证明有用的答案。有人问了这个问题,然后删除了他们的评论:“顺序重要吗?+1对于
OrderedSet
recipe,但是OP可能不需要所有的集合操作+1对于
OrderedSet
recipe,但是OP可能不需要所有的集合操作。”