大多数用python语言从单列表构建字典的方法

时间:2021-10-13 22:09:17

I have a list of day names (typically Monday-Saturday, though special cases apply) that I want to create a dictionary out of. I want to initialize the value of each day to zero.

我有一个日名的列表(通常是周一到周六,尽管有特殊情况),我想用它来创建字典。我想把每天的值初始化为零。

If I had a list of zeroes the same length of the list of days, this would be a simple use case of zip(). However, a list of zeroes is a waste of space, and if that were the only solution I'd just as soon do something like:

如果我有一个相同长度的0的列表,这将是一个简单的zip()用例。然而,一个0的列表是在浪费空间,如果这是唯一的解决方案,我马上就会做一些类似的事情:

for day in weekList:    dayDict[day] = 0

Is there a more pythonic way?

有没有更符合python的方法?

2 个解决方案

#1


17  

Apart from dict.fromkeys you can also use dict-comprehension, but fromkeys() is faster than dict comprehensions:

除了dict.fromkeys,您还可以使用dict-comprehension,但是fromkeys()比dict comprehension更快:

In [27]: lis = ['a', 'b', 'c', 'd']In [28]: dic = {x: 0 for x in lis}In [29]: dicOut[29]: {'a': 0, 'b': 0, 'c': 0, 'd': 0}

For 2.6 and earlier:

2.6和更早的:

In [30]: dic = dict((x, 0) for x in lis)In [31]: dicOut[31]: {'a': 0, 'b': 0, 'c': 0, 'd': 0}

timeit comparisons:

时间比较:

In [38]: %timeit dict.fromkeys(xrange(10000), 0)         # winner1000 loops, best of 3: 1.4 ms per loopIn [39]: %timeit {x: 0 for x in xrange(10000)}100 loops, best of 3: 2.08 ms per loopIn [40]: %timeit dict((x, 0) for x in xrange(10000))100 loops, best of 3: 4.63 ms per loop

As mentioned in comments by @Eumiro and @mgilson it is important to note that fromkeys() and dict-comprehensions may return different objects if the values used are mutable objects:

正如@Eumiro和@mgilson在评论中提到的,重要的是要注意,如果所使用的值是可变对象,则fromkeys()和dict-可能返回不同的对象:

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 objectIn [44]: dic = {x: [] for x in lis}In [45]: [id(x) for x in dic.values()]Out[45]: [165420780, 165420940, 163062700, 163948812]  # unique objects

#2


25  

Use the .fromkeys() class method:

使用.fromkeys()类方法:

dayDict = dict.fromkeys(weekList, 0)

It builds a dictionary using the elements from the first argument (a sequence) as the keys, and the second argument (which defaults to None) as the value for all entries.

它使用第一个参数(序列)中的元素作为键,第二个参数(默认为None)作为所有条目的值,构建一个字典。

By it's very nature, this method will reuse the value for all keys; don't pass it a mutable value such as a list or dict and expect it to create separate copies of that mutable value for each key. In that case, use a dict comprehension instead:

由于它的本质,这个方法将重用所有键的值;不要给它传递一个可变值,如列表或dict类型,并期望它为每个键创建该可变值的独立副本。在这种情况下,可以使用一种词典理解:

dayDict = {d: [] for d in weekList}

#1


17  

Apart from dict.fromkeys you can also use dict-comprehension, but fromkeys() is faster than dict comprehensions:

除了dict.fromkeys,您还可以使用dict-comprehension,但是fromkeys()比dict comprehension更快:

In [27]: lis = ['a', 'b', 'c', 'd']In [28]: dic = {x: 0 for x in lis}In [29]: dicOut[29]: {'a': 0, 'b': 0, 'c': 0, 'd': 0}

For 2.6 and earlier:

2.6和更早的:

In [30]: dic = dict((x, 0) for x in lis)In [31]: dicOut[31]: {'a': 0, 'b': 0, 'c': 0, 'd': 0}

timeit comparisons:

时间比较:

In [38]: %timeit dict.fromkeys(xrange(10000), 0)         # winner1000 loops, best of 3: 1.4 ms per loopIn [39]: %timeit {x: 0 for x in xrange(10000)}100 loops, best of 3: 2.08 ms per loopIn [40]: %timeit dict((x, 0) for x in xrange(10000))100 loops, best of 3: 4.63 ms per loop

As mentioned in comments by @Eumiro and @mgilson it is important to note that fromkeys() and dict-comprehensions may return different objects if the values used are mutable objects:

正如@Eumiro和@mgilson在评论中提到的,重要的是要注意,如果所使用的值是可变对象,则fromkeys()和dict-可能返回不同的对象:

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 objectIn [44]: dic = {x: [] for x in lis}In [45]: [id(x) for x in dic.values()]Out[45]: [165420780, 165420940, 163062700, 163948812]  # unique objects

#2


25  

Use the .fromkeys() class method:

使用.fromkeys()类方法:

dayDict = dict.fromkeys(weekList, 0)

It builds a dictionary using the elements from the first argument (a sequence) as the keys, and the second argument (which defaults to None) as the value for all entries.

它使用第一个参数(序列)中的元素作为键,第二个参数(默认为None)作为所有条目的值,构建一个字典。

By it's very nature, this method will reuse the value for all keys; don't pass it a mutable value such as a list or dict and expect it to create separate copies of that mutable value for each key. In that case, use a dict comprehension instead:

由于它的本质,这个方法将重用所有键的值;不要给它传递一个可变值,如列表或dict类型,并期望它为每个键创建该可变值的独立副本。在这种情况下,可以使用一种词典理解:

dayDict = {d: [] for d in weekList}