简体   繁体   English

对列表中所有项目传递功能的最快方法

[英]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: 这里发生的是:

  • We apply the function to each element in word_list , returning a map object 我们将函数应用于word_list每个元素,返回一个map对象
  • Combine it into a sequence ( zip object) of one-to-one element tuples from the original list and that map object 将其组合为原始列表和该映射对象中的一对一元素元组的序列( zip对象)
  • Convert the resulting sequence into a dictionary 将结果序列转换成字典

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.

相关问题 从列表中删除所有多个事件项的最快方法? - Fastest way to remove all multiple occurrence items from a list? 在 Python 列表中查找最大化/最小化函数的所有元素的最快方法 - Fastest way to find all elements that maximize / minimize a function in a Python list 生成一个数字的所有除数列表的最快方法 - fastest way to produce a list of all divisors of a number 列表中所有字符串的长度:最快的方法 - Length of all string in the list: the fastest way 列出 N 以下所有素数的最快方法 - Fastest way to list all primes below N 从列表中删除与子字符串匹配的项目的最快方法 - Python - The fastest way to remove items that matches a substring from list - Python 检查列表中是否正好有n个项目与python中的条件匹配的最快方法 - Fastest way to check if exactly n items in a list match a condition in python 查找Python中所有解决方案列表中是否存在一组潜力的最快方法 - Fastest way to find if a Group of potentials exists in a list of all solutions in Python 检查字符串列表中的所有元素是否都在字符串中的最快方法 - fastest way to check if all elements of a list of strings is in a string 识别物品组合的最快方法 - Fastest way to identify the combinations of items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM