[英]Fastest way to pass function on all items in list
I have a list of about 50,000 or so words, and I want to pass a function on each item in the list. 我有一个大约50,000个单词的列表,我想在列表中的每个项目上传递一个函数。 Then I want to save the original word as a key, and the translated word as the respective value in a dictionary.
然后,我想将原始单词另存为键,将翻译后的单词另存为字典中的值。 Right now I know I can do this:
现在我知道我可以这样做:
translations = {word: translate(word) for word in word_list}
But this takes too long I think. 但是我认为这花费了太长时间。 Is there a faster way this can be accomplished?
有没有更快的方法可以做到这一点?
Mapping functions should work faster than dict comprehensions: 映射函数的工作速度应比dict理解速度更快:
translations = dict(zip(word_list, map(translate, word_list)))
What happens here is: 这里发生的是:
word_list
, returning a map
object word_list
每个元素,返回一个map
对象 zip
object) of one-to-one element tuples from the original list and that map object zip
对象) After setting up a test program, it appears that there is a slight performance improvement. 设置测试程序后,似乎性能略有改善。 This is the code:
这是代码:
from datetime import datetime
def translate(wo):
return wo.upper()
word_list = {str(i):str(i+1) for i in range(50000)}
d = datetime.now()
translations = dict(zip(word_list, map(translate, word_list)))
print(datetime.now() - d)
d = datetime.now()
translations = {word: translate(word) for word in word_list}
print(datetime.now() - d)
After a few runs, the second printed time is always greater than the first one, which proves the efficiency. 经过几次运行,第二个打印时间总是大于第一个,证明了效率。
If you only need few values, and won't iterate over the dict, you can try doing it lazily: 如果您只需要很少的值,并且不会遍历该字典,则可以延迟进行尝试:
class MyDefaultDict(dict):
def __init__(self, word_iterable, translate):
self.word_set = frozenset(word_iterable)
self.translate = translate
def __missing__(self, key):
if key in self.word_set:
translated = translate(key)
self[key] = translated
return translated
raise KeyError(key)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.