python dict.fromkeys()研究

来源:互联网 发布:mac上一页快捷键 编辑:程序博客网 时间:2024/05/22 03:33

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)32,>>> d = {}.fromkeys(l).keys()>>> print d[1, 2, 3]>>> len(d)3

还可对list和string去重

0 0
原创粉丝点击