def unique(seq): #return [x for x in my_list if x not in locals()['_[1]']] return {}.fromkeys(seq).keys() dict.fromkeys(seq,val=None) #创建并返回一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值(默认为None)
例子:1, >>> l = [1,2,3] >>> a3 = {}.fromkeys(l) >>> print a3 {1: None, 2: None, 3: None} >>> len(a3) 3 2,>>> d = {}.fromkeys(l).keys() >>> print d [1, 2, 3] >>> len(d) 3
还可对list和string去重