迭代Python3地图的最快方法

时间:2022-03-18 18:18:03

I recently asked this question, and I modified one of the solutions, which uses a loop, to use a map instead. Of course, in Python3, map() returns an iterator and will not execute the callback until it is iterated over. Consider the following dictionary and mapping functions:

我最近问了这个问题,我修改了一个使用循环的解决方案,而不是使用地图。当然,在Python3中,map()返回一个迭代器,并且在迭代之前不会执行回调。考虑以下字典和映射函数:

files = {'Code.py': 'Stan', 'Output.txt': 'Randy', 'Input.txt': 'Randy'}    

is the dictionary

是字典

mapped = {}

for k, v in files.items():
    mapped.setdefault(v, []).append(k)

Runs almost twice as fast as:

运行速度几乎是以下的两倍:

mapped = {}

any(map(lambda i: mapped.setdefault(i[1],[]).append(i[0]),files.items()))

My (uneducated) guess is that building the map object and then iterating over it is just less efficient than performing the callback in a real loop. My question is: Is there a better way to use map that either executes the callback on build, or is there a faster way to iterate over the map than using any? For reference, here are the runtimes of each:

我(未受过教育)的猜测是构建地图对象然后迭代它只比在实际循环中执行回调效率低。我的问题是:有没有更好的方法来使用在构建时执行回调的map,还是有更快的方法迭代地图而不是使用any?供参考,以下是每个的运行时间:

#timeit
#using setdefault with any, lambda, map:
#1.46 µs ± 12.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
#using setdefault with loop:
#826 ns ± 2.75 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

1 个解决方案

#1


0  

The answer to this question is simple, thanks to @Aran-Fey for schooling me. This is not a good use case for map and any, because the loop isn't meant to return anything, rather, it is meant to modify the dictionary in place. Using map and any has the unwanted side effects of creating a map object and returning a boolean after iterating over it, neither of which are necessary. Apart from being less readable, my "one liner" produces two unwanted outputs, while the plain loop does no such thing. So on top of being only half as fast at runtime, my method also has these unwanted side effects and therefore should not be used, plain and simple.

这个问题的答案很简单,感谢@ Aran-Fey让我上学。这不是map和any的一个很好的用例,因为循环并不意味着返回任何东西,而是意味着修改字典到位。使用map和any有创建一个map对象的不必要的副作用,并在迭代之后返回一个布尔值,这两者都不是必需的。除了不太可读之外,我的“一个班轮”产生两个不需要的输出,而普通循环没有这样的东西。因此,除了在运行时只有一半的速度之外,我的方法也有这些不必要的副作用,因此不应该使用,简单明了。

#1


0  

The answer to this question is simple, thanks to @Aran-Fey for schooling me. This is not a good use case for map and any, because the loop isn't meant to return anything, rather, it is meant to modify the dictionary in place. Using map and any has the unwanted side effects of creating a map object and returning a boolean after iterating over it, neither of which are necessary. Apart from being less readable, my "one liner" produces two unwanted outputs, while the plain loop does no such thing. So on top of being only half as fast at runtime, my method also has these unwanted side effects and therefore should not be used, plain and simple.

这个问题的答案很简单,感谢@ Aran-Fey让我上学。这不是map和any的一个很好的用例,因为循环并不意味着返回任何东西,而是意味着修改字典到位。使用map和any有创建一个map对象的不必要的副作用,并在迭代之后返回一个布尔值,这两者都不是必需的。除了不太可读之外,我的“一个班轮”产生两个不需要的输出,而普通循环没有这样的东西。因此,除了在运行时只有一半的速度之外,我的方法也有这些不必要的副作用,因此不应该使用,简单明了。