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

是否有任何內置方法可以在 python 中獲取可迭代的

Is there any built-in way to get the length of an iterable in python?(是否有任何內置方法可以在 python 中獲取可迭代的長度?)
本文介紹了是否有任何內置方法可以在 python 中獲取可迭代的長度?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

例如,Python 中的文件是可迭代的——它們迭代文件中的行.我想計算行數.

For example, files, in Python, are iterable - they iterate over the lines in the file. I want to count the number of lines.

一種快速的方法是這樣做:

One quick way is to do this:

lines = len(list(open(fname)))

但是,這會將整個文件加載到內存中(一次).這反而違背了迭代器的目的(只需要將當前行保留在內存中).

However, this loads the whole file into memory (at once). This rather defeats the purpose of an iterator (which only needs to keep the current line in memory).

這不起作用:

lines = len(line for line in open(fname))

因為生成器沒有長度.

除了定義一個計數函數之外,還有什么方法可以做到這一點嗎?

Is there any way to do this short of defining a count function?

def count(i):
    c = 0
    for el in i: c += 1
    return c

為了澄清,我知道必須閱讀整個文件!我只是不想一下子把它放在內存中

To clarify, I understand that the whole file will have to be read! I just don't want it in memory all at once

推薦答案

沒有遍歷iterable并計算迭代次數,沒有.這就是使它成為可迭代而不是列表的原因.這甚至不是一個特定于 python 的問題.看看經典的鏈表數據結構.查找長度是一個 O(n) 操作,涉及迭代整個列表以查找元素的數量.

Short of iterating through the iterable and counting the number of iterations, no. That's what makes it an iterable and not a list. This isn't really even a python-specific problem. Look at the classic linked-list data structure. Finding the length is an O(n) operation that involves iterating the whole list to find the number of elements.

正如上面提到的 mcrute,您可能可以將您的功能簡化為:

As mcrute mentioned above, you can probably reduce your function to:

def count_iterable(i):
    return sum(1 for e in i)

當然,如果您要定義自己的可迭代對象,您始終可以自己實現 __len__ 并在某處保留元素計數.

Of course, if you're defining your own iterable object you can always implement __len__ yourself and keep an element count somewhere.

這篇關于是否有任何內置方法可以在 python 中獲取可迭代的長度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲久视频 | 91高清免费观看 | 国产精品久久久久久久7777 | 亚洲日韩欧美一区二区在线 | 亚洲综合一区二区三区 | 成年人在线视频 | 欧美性tv | 欧美国产日韩一区 | 久久久国产一区二区三区四区小说 | 日韩有码在线观看 | 99re视频在线 | 久热精品在线播放 | 少妇诱惑av | 中文字字幕一区二区三区四区五区 | 国产一级片免费视频 | 欧美日韩一区二区在线播放 | 久草中文网 | 国产在线观看一区 | 日韩在线视频观看 | 亚洲激精日韩激精欧美精品 | 久久伊人影院 | 成人三级在线播放 | 久久久久久99 | 国产精品三级久久久久久电影 | 殴美成人在线视频 | 欧美一区二区三区国产 | 久草a√ | 日韩午夜电影在线观看 | 国产欧美一区二区精品久导航 | 亚洲高清av| 成年人免费看的视频 | 欧美精品一区在线发布 | 99久久精品国产一区二区三区 | 日韩欧美国产一区二区三区 | 国产蜜臀 | 日韩免费视频一区二区 | 国产99精品 | 国产欧美一区二区三区日本久久久 | 拍真实国产伦偷精品 | 欧美一区二区三区大片 | 国产精品三级 |