久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么我要使用 itertools.islice 而不是普通的列表

Why would I want to use itertools.islice instead of normal list slicing?(為什么我要使用 itertools.islice 而不是普通的列表切片?)
本文介紹了為什么我要使用 itertools.islice 而不是普通的列表切片?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我看來,itertools 模塊中的許多函數都有更簡單的等效項.例如,據我所知, itertools.islice(range(10),2,5)range(10)[2:5]itertools.chain([1,2,3],[4,5,6])[1,2,3]+[4,5,6].主文檔頁面提到了速度優勢,但除此之外還有什么理由選擇 itertools?

It seems to me that many functions in the itertools module have easier equivalents. For example, as far as I can tell, itertools.islice(range(10),2,5) does the same thing as range(10)[2:5], and itertools.chain([1,2,3],[4,5,6]) does the same thing as [1,2,3]+[4,5,6]. The main documentation page mentions speed advantages, but are there any reasons to choose itertools besides this?

推薦答案

解決你提出的兩個例子:

To address the two examples you brought up:

import itertools


data1 = range(10)

# This creates a NEW list
data1[2:5]

# This creates an iterator that iterates over the EXISTING list
itertools.islice(data1, 2, 5)


data2 = [1, 2, 3]
data3 = [4, 5, 6]

# This creates a NEW list
data2 + data3

# This creates an iterator that iterates over the EXISTING lists
itertools.chain(data2, data3)

您想要使用迭代器而不是其他方法的原因有很多.如果列表非常大,則創建包含大型子列表的新列表可能會出現問題,或者特別是創建包含其他兩個列表副本的列表.使用 islice()chain() 允許您以您想要的方式迭代列表,而無需使用更多內存或計算來創建新的列表.此外,正如 unutbu 所述,您不能將括號切片或添加與迭代器一起使用.

There are many reasons why you'd want to use iterators instead of the other methods. If the lists are very large, it could be a problem to create a new list containing a large sub-list, or especially create a list that has a copy of two other lists. Using islice() or chain() allows you to iterate over the list(s) in the way you want, without having to use more memory or computation to create the new lists. Also, as unutbu mentioned, you can't use bracket slicing or addition with iterators.

我希望這是一個足夠的答案;還有很多其他答案和其他資源可以解釋為什么迭代器很棒,所以我不想在這里重復所有這些信息.

I hope that's enough of an answer; there are plenty of other answers and other resources explaining why iterators are awesome, so I don't want to repeat all of that information here.

這篇關于為什么我要使用 itertools.islice 而不是普通的列表切片?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 亚洲精品二区 | 亚洲综合区 | 久久九精品| 亚洲永久字幕 | 久久精品久久久久久 | 欧美一级特黄aaa大片在线观看 | 国产又爽又黄的视频 | 久久性av | 亚洲一区二区三区四区视频 | 超碰8 | 久久福利网站 | 国产一区91精品张津瑜 | 亚州精品天堂中文字幕 | 久久国产一区 | 九一国产精品 | 91社区在线观看播放 | 伊人网在线看 | 情侣黄网站免费看 | 日韩av在线中文字幕 | 欧美一区日韩一区 | 精彩视频一区二区三区 | 97中文视频 | av大片在线观看 | 国产美女特级嫩嫩嫩bbb片 | 国产一区二区三区四区五区加勒比 | 懂色av一区二区三区在线播放 | 久久欧美精品 | 国产精品夜间视频香蕉 | 91在线看网站 | 日韩成人在线电影 | 国产精品国产三级国产aⅴ原创 | 中文字幕人成乱码在线观看 | av在线伊人 | 国产精品一区二区三区四区 | 国产亚洲精品成人av久久ww | 国产成人麻豆免费观看 | 国产精品永久免费视频 | 日本成人中文字幕 | 97超碰免费| 色综合天天天天做夜夜夜夜做 | 日韩欧美三区 |