3 回答

TA贡献1871条经验 获得超13个赞
可以使用内置功能对列表进行排序和重复数据删除:
myList = sorted(set(myList))
set 是Python> = 2.3的内置函数
sorted 是Python> = 2.4的内置函数

TA贡献1816条经验 获得超4个赞
如果要保持原始列表的顺序,只需使用OrderedDict与None作为值。
在Python2中:
from collections import OrderedDict
from itertools import izip, repeat
unique_list = list(OrderedDict(izip(my_list, repeat(None))))
在Python3中,它甚至更简单:
from collections import OrderedDict
from itertools import repeat
unique_list = list(OrderedDict(zip(my_list, repeat(None))))
如果您不喜欢迭代器(压缩和重复),则可以使用生成器(在2和3中均可使用):
from collections import OrderedDict
unique_list = list(OrderedDict((element, None) for element in my_list))
添加回答
举报