问题:将迭代器转换为列表的最快方法

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

user_list = [user for user in user_iterator]

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

user_list = [user for user in user_iterator]

回答 0

list(your_iterator)
list(your_iterator)

回答 1

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

user_list = [*your_iterator]

pythonic的方法是:

user_list  = list(your_iterator)

since python 3.5 you can use * iterable unpacking operator:

user_list = [*your_iterator]

but the pythonic way to do it is:

user_list  = list(your_iterator)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。