python dict.fromkeys()研究

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去重

posted @ 2014-05-30 09:58  jianhong  阅读(160)  评论(0编辑  收藏  举报