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

在項目列表上調用一個函數的最簡潔方法

cleanest way to call one function on a list of items(在項目列表上調用一個函數的最簡潔方法)
本文介紹了在項目列表上調用一個函數的最簡潔方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 python 2 中,我使用 map 將函數應用于多個項目,例如,刪除所有匹配模式的項目:

In python 2, I used map to apply a function to several items, for instance, to remove all items matching a pattern:

map(os.remove,glob.glob("*.pyc"))

當然我忽略了os.remove的返回碼,我只想刪除所有文件.它創建了一個列表的臨時實例,但它確實有效.

Of course I ignore the return code of os.remove, I just want all files to be deleted. It created a temp instance of a list for nothing, but it worked.

在 Python 3 中,由于 map 返回的是迭代器而不是列表,因此上面的代碼什么也不做.我找到了一種解決方法,因為 os.remove 返回 None,我使用 any 強制對完整列表進行迭代,而不創建 列表(性能更好)

With Python 3, as map returns an iterator and not a list, the above code does nothing. I found a workaround, since os.remove returns None, I use any to force iteration on the full list, without creating a list (better performance)

any(map(os.remove,glob.glob("*.pyc")))

但這似乎有點危險,特別是在將其應用于返回某些內容的方法時.另一種使用單行而不創建不必要列表的方法?

But it seems a bit hazardous, specially when applying it to methods that return something. Another way to do that with a one-liner and not create an unnecessary list?

推薦答案

map()(以及從 2.7 到 3.x 的許多其他函數)返回生成器而不是列表的變化是一種節省內存的技術.在大多數情況下,更正式地寫出循環不會降低性能(它甚至可能更適合于可讀性).

The change from map() (and many other functions from 2.7 to 3.x) returning a generator instead of a list is a memory saving technique. For most cases, there is no performance penalty to writing out the loop more formally (it may even be preferred for readability).

我會提供一個例子,但@vaultah 在評論中指出:仍然是單線:

I would provide an example, but @vaultah nailed it in the comments: still a one-liner:

for x in glob.glob("*.pyc"): os.remove(x)

這篇關于在項目列表上調用一個函數的最簡潔方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美aaa级 | 亚洲精品视频免费观看 | 日韩视频一区 | 亚洲综合色站 | www久久| 欧美在线视频免费 | 男女羞羞在线观看 | 中文字幕亚洲视频 | 99热激情| 久草免费在线视频 | 国产一区二区在线免费播放 | 中文字幕视频在线 | 成人av一区二区亚洲精 | 99热视 | 在线免费观看欧美 | 好姑娘高清在线观看电影 | 久久免费精品视频 | 日日操操 | 国产三区av | 午夜精品一区二区三区在线观看 | 国产欧美在线视频 | 亚洲一区二区精品视频 | 欧美一区2区三区4区公司 | 国产精品区一区二区三区 | 九九热在线视频 | 国产午夜精品一区二区三区 | 二区三区视频 | 亚洲在线高清 | 伊人在线 | 亚洲精品视频一区 | 欧美精品一区二区三区在线播放 | 最新免费av网站 | 在线国产一区二区 | 国产一区二区三区视频 | 国产色99| 久久精品国产v日韩v亚洲 | 天天操妹子 | av在线播放一区二区 | 宅女噜噜66国产精品观看免费 | 久久精品视频免费观看 | 日本午夜在线视频 |