一、keys()方法
f = [5, 7, 3, 3, 2, 7]
i = list({}.fromkeys(f).keys())
print (i)
结果为[5, 7, 3, 2],所得结果未重新排序
Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。语法为:
dict.fromkeys(seq[, value])
例子:
seq = ('Google', 'Runoob', 'Taobao')
dict = dict.fromkeys(seq)
print "新字典为 : %s" % str(dict)
dict = dict.fromkeys(seq, 10)
print "新字典为 : %s" % str(dict)
结果为:
新字典为 : {'Google': None, 'Taobao': None, 'Runoob': None}
新字典为 : {'Google': 10, 'Taobao': 10, 'Runoob': 10}
二、set()方法
f = [5, 7, 3, 3, 2, 7]
i = set(f)
print (i)
结果为{2, 3, 5, 7},所得结果重新排序,且返回对象为set,而非list.