简体   繁体   中英

Remove repeated item in list in python

Let say I have a list of

['a','man', 'and', 'a', 'woman']

How do I remove the repeated 'a' so that it will only be:

['a','man', 'and', 'woman']

Keeps order:

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

If the order is not important, then you can just do:

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

如果顺序很重要,类似@ jamylak的一个建议,就是用这个 OrderedSet配方。

list(OrderedSet(L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM