久久久久久久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 對象)
主站蜘蛛池模板: 久艹网站 | 国产精品视频免费观看 | 欧美色综合一区二区三区 | 一区在线播放 | 国产精品欧美一区二区三区不卡 | 久久久久久国产 | 亚洲欧美日韩中文字幕一区二区三区 | 国产精品99久久免费观看 | caoporn免费在线视频 | 综合精品久久久 | 久久久妇女国产精品影视 | 亚洲午夜精品 | 久久国产精品一区二区 | 羞羞视频在线免费 | 97视频成人 | 久久久蜜桃 | 黄色网址在线免费观看 | 国产一级免费视频 | 欧美精品一区在线发布 | 国产精品美女久久久久久免费 | 亚洲成人精品视频 | 国产视频91在线 | 麻豆视频在线免费看 | av午夜电影| 日韩一区二区三区在线 | 久久成人综合 | 性一区 | 久久成人免费 | 欧洲视频一区二区 | www.久久久久久久久 | 国产欧美一区二区三区在线看 | 亚洲精品456 | www.亚洲一区二区三区 | 人人艹人人 | 日韩精品免费播放 | 亚洲电影一区 | 欧美一区二区三区高清视频 | 日韩精品免费一区二区在线观看 | 亚洲欧美精品在线 | 激情亚洲 | 亚洲视频中文字幕 |