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

使用 Python 對包含“無"的列表求和

Sum a list which contains #39;None#39; using Python(使用 Python 對包含“無的列表求和)
本文介紹了使用 Python 對包含“無"的列表求和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

基本上我的問題是說您有一個包含無"的列表,您將如何嘗試檢索列表的總和.下面是一個我試過的例子,它不起作用,我得到了錯誤:TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'.謝謝

Basically my question is say you have an list containing 'None' how would you try retrieving the sum of the list. Below is an example I tried which doesn't work and I get the error: TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'. Thanks

def sumImport(self):
    my_list = [[1,2,3,None],[1,2,3],[1,1],[1,1,2,2]]
    k = sum(chain.from_iterable(my_list))
    return k

推薦答案

可以使用filter功能

>>> sum(filter(None, [1,2,3,None]))
6

根據評論更新

filter 的典型用法是 filter(func, iterable),但是將 None 作為第一個參數傳遞是一種特殊情況,在 Python 文檔.引用:

Typically filter usage is filter(func, iterable), but passing None as first argument is a special case, described in Python docs. Quoting:

如果function為None,則假定恒等函數,即iterable中所有為false的元素都被移除.

If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

這篇關于使用 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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 天堂网色| 密色视频| 成人黄色在线观看 | 亚洲精品日韩一区二区电影 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 亚洲精品在线免费 | 日本成人综合 | jizz在线免费观看 | 午夜国产一级 | 在线一区二区三区 | 亚洲国产精品久久 | 99国产精品久久久久 | 成人妇女免费播放久久久 | 在线国产一区二区 | 精品不卡| 人成在线| 日本精品视频在线观看 | 免费a v网站 | 噜噜噜噜狠狠狠7777视频 | 日韩视频在线免费观看 | 日韩欧美三区 | 久久精品一二三影院 | 久草网站 | 在线只有精品 | 欧美日韩在线免费观看 | 欧洲一区在线观看 | 欧美在线一区二区三区 | 国产偷录视频叫床高潮对白 | 国产成人在线播放 | 北条麻妃99精品青青久久 | 久久中文字幕一区 | 欧美日本韩国一区二区 | 日韩欧美三区 | 91九色porny首页最多播放 | 日本 欧美 三级 高清 视频 | 成人欧美一区二区三区在线观看 | 美国一级毛片a | 亚洲欧美另类在线观看 | wwwxx在线观看| 欧美日韩在线视频观看 | 成人免费观看男女羞羞视频 |