Python 将迭代器转换为列表的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3790848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:46:27  来源:igfitidea点击:

Fastest way to convert an iterator to a list

pythoniteratorlist-comprehension

提问by systempuntoout

Having an iteratorobject, is there something faster, better or more correct than a list comprehension to get a list of the objects returned by the iterator?

拥有一个iterator对象,是否有比列表理解更快、更好或更正确的方法来获取迭代器返回的对象列表?

user_list = [user for user in user_iterator]

采纳答案by mikerobi

list(your_iterator)

回答by kederrac

since python 3.5you can use *iterable unpacking operator:

python 3.5 开始,您可以使用*可迭代的解包运算符:

user_list = [*your_iterator]

but the pythonic wayto do it is:

但是pythonic的方法是:

user_list  = list(your_iterator)