Python之list對應元素求和

  本次分享將講述如何在Python中對多個list的對應元素求和,前提是每個list的長度一樣。比如:a=[1,2,3], b=[2,3,4], c=[3,4,5], 對a,b,c的對應元素求和,輸出應為[6,9,12].
  
方法一:
  直接求解,按照對應元素相加的原則,可先定義一個函數。

def list_add(a,b):c = []for i in range(len(a)):c.append(a[i]+b[i])return cif __name__ == '__main__':a = [1,2,3]b = [2,3,4]c = [3,4,5]print(list_add(list_add(a,b),c))

方法二:
  利用numpy模塊求解。

import numpy as np
a = np.array([1,2,3])
b = np.array([2,3,4])
c = np.array([3,4,5])
print(a+b+c)

需要注意的是,a+b+c后的類型為numpy.ndarray.
方法三:
  利用numpy模塊的sum()函數進行求解。

import numpy as np
a = [1,2,3]
b = [2,3,4]
c = [3,4,5]
print(np.sum([a,b,c], axis = 0))

其中的axis參數表示縱向求和。



本次分享到此結束,歡迎大家交流~~

注意:本人現已開通兩個微信公眾號: 因為Python(微信號為:python_math)以及輕松學會Python爬蟲(微信號為:easy_web_scrape), 歡迎大家關注哦~~

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540560.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540560.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540560.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[轉載] Python中str跟int的轉換

參考鏈接: Python中的類型轉換 字符串str轉換成int: int_value int(str_value) int轉換成字符串str: str_value str(int_value) a100 b666 #int轉str類型 print(int轉str類型) print(int轉str: str(a)) #str轉int類型 print(str轉int類型…

ot協議是什么_OT的完整形式是什么?

ot協議是什么OT:主題外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…

[轉載] python中字符串編碼形式及其所占字節

參考鏈接: Python中的字節對象與字符串 1.常見字符串編碼錯誤 在使用Python讀文件時經常遇到編碼問題引起的錯誤,比如: UnicodeDecodeError: gbk codec cant decode byte 0x80 in position 30: illegal multibyte sequence 遇到這種異…

[AtCoder-ARC073F]Many Moves

題目大意:   有一排n個格子和2枚硬幣。   現在有q次任務,每一次要你把其中一枚硬幣移到x的位置上,移動1格的代價是1。   兩枚硬幣不能同時移動,任務必須按次序完成。   現在告訴你兩枚硬幣初始狀態所在的位置a和b&#xf…

ScalavsKotlin

Is Scala better that Kotlin? No..., Is Kotlin better than Scala? No... Scala比Kotlin更好嗎? 不...,Kotlin勝過Scala嗎? 沒有... Both programming languages have their own profits and are for a specific set of development. It…

工業智能相機與基于PC的機器視覺的區別比較

隨著科技的日漸成熟,機器視覺得到了飛速發展。由于嵌入式技術的發展,近幾年智能相機性能顯著提高,越來越多必須依賴于PC處理的應用開始向智能相機平臺傾斜。低成本、高可靠性及易于安裝維護等優勢,使得機器視覺在制造業上的規模性應用越來越普…

[轉載] python skimage在圖像處理中的用法

參考鏈接: 在Python中打印單變量和多變量 基于python腳本語言開發的數字圖片處理包,比如PIL,Pillow, opencv, scikit-image等。 PIL和Pillow只提供最基礎的數字圖像處理,功能有限;opencv實際上是一個c庫,只是提供了py…

scala元組 數組_Scala中的數組

scala元組 數組Scala中的數組 (Arrays in Scala) An array is a linear data structure with a fixed number of elements. It is a collection that stores a fixed number Arrays in Scalf elements of the same datatype. In Scala, an array is 0 indexed, i.e. the first …

OpenStack —— DevStack一鍵自動化安裝

一、DevStack介紹Devstack目前是支持Ubuntu16.04和CentOS 7,而且Devstack官方建議使用Ubuntu16.04,所以我們使用Ubuntu 16.04進行安裝。默認無論是Devstack和OpenStack,都是采用Master的代碼進行安裝,這樣經常會出現,今…

[轉載] Python學習筆記——運維和Shell

參考鏈接: 在C / C,Python,PHP和Java中交換兩個變量 目錄 什么是運維 運維第一工具-shell編程 shell歷史 執行腳本 基本語法 Shell腳本語法 條件測試:test [ if/then/elif/else/fi case/esac for/do/done …

scala java混合_Scala特性混合

scala java混合Scala | 特性混合 (Scala | Trait Mixins ) In Scala, the number of traits can be extended using a class or an abstract class. This is known as Trait Mixins. For extending, only traits, the blend of traits, class or abstract class are valid. If …

Scala鑄造

Scala中的類型 (Types in Scala) Type also know as data type tells the compiler about the type of data that is used by the programmer. For example, if we initialize a value or variable as an integer the compiler will free up 4 bytes of memory space and it wi…

/ 卡路里_最大卡路里

/ 卡路里Problem statement: 問題陳述: Shivang is very foodie but he has a diet plan. He has an array of elements indicating the calorie of food he can consume on that day. In his diet plan, he can’t eat on for three consecutive days. But since …

[轉載] Python類中的私有變量和公有變量

參考鏈接: Python中的私有變量 我們這里就直奔主題,不做基礎鋪墊,默認你有一些Python類的基礎,大家在看這篇博客的時候,如果基礎知識忘了,可以去菜鳥教程 從一個簡單的類開始 class A(): #定義一…

OpenCV探索之路(二十五):制作簡易的圖像標注小工具

搞圖像深度學習的童鞋一定碰過圖像數據標注的東西,當我們訓練網絡時需要訓練集數據,但在網上又沒有找到自己想要的數據集,這時候就考慮自己制作自己的數據集了,這時就需要對圖像進行標注。圖像標注是件很枯燥又很費人力物力的一件…

固件的完整形式是什么?

FW:前進 (FW: Forward) FW is an abbreviation of "Forward". FW是“ Forward”的縮寫 。 It is an expression, which is commonly used in Gmail or messaging platform. It is also written as FWD or Fwd or Fw. It shows that the email has been s…

[轉載] python __slots__ 詳解(上篇)

參考鏈接: Python的__name __(特殊變量) python中的new-style class要求繼承Python中的一個內建類型, 一般繼承object,也可以繼承list或者dict等其他的內建類型。 在python新式類中,可以定義一個變量__slots__,它的作…

委托BegionInvoke和窗體BegionInvoke

委托BegionInvoke是指通過委托方法執行多線程任務,例如: //定義委托成員變量 delegate void dg_DeleAirport(); //指定委托函數 dg_DeleAirport dga AirportBLL.DeleteHistoryTransAirport; //通過BeginInvoke以異步線程方式執行委托函數,可…

圖論 弦_混亂的弦

圖論 弦Problem statement: 問題陳述: You are provided an input string S and the string "includehelp". You need to figure out all possible subsequences "includehelp" in the string S? Find out the number of ways in which the s…

[轉載] Python列表操作

參考鏈接: Python中的基本運算符 Python列表: 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推; Python有6個序列的…