Most Pythonic Way to Build Dictionary From Single List
我有一个日名列表(通常是星期一到星期六,尽管有特殊情况),我想用它创建一个字典。我想将每天的值初始化为零。
如果我有一个与天数列表长度相同的零列表,那么这将是一个简单的用例
1 2 | for day in weekList: dayDict[day] = 0 |
有没有更像Python的方法?
使用类方法:
1 | dayDict = dict.fromkeys(weekList, 0) |
它使用第一个参数(序列)中的元素作为键,第二个参数(默认为
从本质上讲,此方法将重用所有键的值;不要将可变值(如列表或dict)传递给它,并希望它为每个键创建该可变值的单独副本。在这种情况下,使用听写理解代替:
1 | dayDict = {d: [] for d in weekList} |
除了
1 2 3 4 5 6 | In [27]: lis = ['a', 'b', 'c', 'd'] In [28]: dic = {x: 0 for x in lis} In [29]: dic Out[29]: {'a': 0, 'b': 0, 'c': 0, 'd': 0} |
对于2.6及更早版本:
1 2 3 4 | In [30]: dic = dict((x, 0) for x in lis) In [31]: dic Out[31]: {'a': 0, 'b': 0, 'c': 0, 'd': 0} |
比较:
1 2 3 4 5 6 7 8 | In [38]: %timeit dict.fromkeys(xrange(10000), 0) # winner 1000 loops, best of 3: 1.4 ms per loop In [39]: %timeit {x: 0 for x in xrange(10000)} 100 loops, best of 3: 2.08 ms per loop In [40]: %timeit dict((x, 0) for x in xrange(10000)) 100 loops, best of 3: 4.63 ms per loop |
正如@eumiro和@mgilson在评论中提到的,需要注意的是,如果使用的值是可变对象,则
1 2 3 4 5 6 7 8 9 | In [42]: dic = dict.fromkeys(lis, []) In [43]: [id(x) for x in dic.values()] Out[43]: [165420716, 165420716, 165420716, 165420716] # all point to a same object In [44]: dic = {x: [] for x in lis} In [45]: [id(x) for x in dic.values()] Out[45]: [165420780, 165420940, 163062700, 163948812] # unique objects |