简体   繁体   English

改善Python代码段性能

[英]Improving Python Snippet Performance

This statement is running quite slowly, and I have run out of ideas to optimize it. 这句话运行得很慢,我已经没有想法来优化它。 Could someone help me out? 有人可以帮帮我吗?

[dict(zip(small_list1, small_list2)) for small_list2 in really_huge_list_of_list]

The small_list s contain only about 6 elements. small_list只包含大约6个元素。

A really_huge_list_of_list of size 209,510 took approximately 16.5 seconds to finish executing. 一个really_huge_list_of_list大小209510中花了大约16.5秒执行完毕。

Thank you! 谢谢!

Edit: 编辑:

really_huge_list_of_list is a generator. really_huge_list_of_list是一个生成器。 Apologies for any confusion. 为任何困惑道歉。 The size is obtained from the result list. 大小从结果列表中获得。

Possible minor improvement: 可能的小改进:

[dict(itertools.izip(small_list1, small_list2)) for small_list2 in really_huge_list_of_list]

Also, you may consider to use generator instead of list comprehensions. 此外,您可以考虑使用生成器而不是列表推导。

To expand on what the comments are trying to say, you should use a generator instead of that list comprehension. 要扩展评论试图说的内容,您应该使用生成器而不是列表理解。 Your code currently looks like this: 您的代码目前看起来像这样:

[dict(zip(small_list1, small_list2)) for small_list2 in really_huge_list_of_list]

and you should change it to this instead: 你应该改为:

def my_generator(input_list_of_lists):
    small_list1 = ["wherever", "small_list1", "comes", "from"]
    for small_list2 in input_list_of_lists:
        yield dict(zip(small_list1, small_list2))

What you're doing right now is taking ALL the results of iterating over your really huge list, and building up a huge list of the results, before doing whatever you do with that list of results. 你现在正在做的是在对你的结果列表进行任何操作之前,将所有结果迭代到你真正庞大的列表上,然后构建一个巨大的结果列表。 Instead, you should turn that list comprehension into a generator so that you never have to build up a list of 200,000 results. 相反,您应该将该列表理解转换为生成器,这样您就不必构建包含200,000个结果的列表。 It's building that result list that's taking up so much memory and time. 它构建的结​​果列表占用了大量的内存和时间。

... Or better yet, just turn that list comprehension into a generator comprehension by changing its outer brackets into parentheses: ...或者更好的是,只需将其外部括号更改为括号,即可将列表理解转换为生成器理解:

(dict(zip(small_list1, small_list2)) for small_list2 in really_huge_list_of_list)

That's really all you need to do. 这真的是你需要做的。 The syntax for list comprehensions and generator comprehensions is almost identical, on purpose: if you understand a list comprehension, you'll understand the corresponding generator comprehension. 列表推导和生成器理解的语法几乎完全相同,如果您理解列表理解,您将理解相应的生成器理解。 (In this case, I wrote out the generator in "long form" first so that you'd see what that comprehension expands to). (在这种情况下,我首先以“长形式”写出了生成器,这样你就可以看到那种理解扩展到了什么)。

For more on generator comprehensions, see here , here and/or here . 有关发电机理解的更多信息,请参见此处此处和/或此处

Hope this helps you add another useful tool to your Python toolbox! 希望这可以帮助您为Python工具箱添加另一个有用的工具!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM