推导式是个绝妙的东西,列表推导式一出,map、filter等函数黯然失色,自 Python2.7以后的版本,此特性扩展到了字典和集合身上,构建字典对象无需调用 dict 方法。
numbers = [1,2,3] d = dict([(number,number*2) for number in numbers])
good
numbers = [1, 2, 3] d = {number: number * 2 for number in numbers}
推导式是个绝妙的东西,列表推导式一出,map、filter等函数黯然失色,自 Python2.7以后的版本,此特性扩展到了字典和集合身上,构建字典对象无需调用 dict 方法。
numbers = [1,2,3] d = dict([(number,number*2) for number in numbers])
good
numbers = [1, 2, 3] d = {number: number * 2 for number in numbers}