如何在 Python 中相加兩個列表

在本主題中,我們將學習如何在 Python 中相加兩個列表。但是在瀏覽主題之前,我們需要了解 Python 中的術語 List 。 Python 列表用於在一個變數中存儲多個項目。列表中的項目可以是任何有序的、可變的,並且允許存儲重複的值。列表的每個項目都有一個適當的索引值,其中列表的第一個索引以{0}開頭,列表的長度索引必須是 n-1。列表中的每一項都由逗號(,)符號分隔,並包含在方括弧[]內。

語法


L1 = ["Apple", 10, "NEW York"] # different data type elements
L2 = [1, 2, 4, 5] # same data type elements

這裡,L1 和 L2 是包含列表中相同或不同數據類型元素的兩個列表。列表 L1 包含 int 和字元串數據類型元素,而列表 L2 只包含 int 數據類型元素。

讓我們考慮一個用 Python 列印列表的程序。

程序列表. py】t1


List1 = ["Rose", "Lotus", 24, "Gold", "USA" ] # define the list
# define the Department Dept2 list
Dept2 = ["Web Designing", 40, 20]
# define the HR_CS
HR_CS = [58, "Ms Wiley"]
List2 = [1, 2, 4, 5, 6] # integer list
print (" Display the List1", List1)
print (" Display the List2", List2)
print (" Display the Department List", Dept2)
print (" Display the CS Department ", HR_CS)

輸出:

Display the List1 ['Rose', 'Lotus', 24, 'Gold', 'USA']
Display the List2 [1, 2, 4, 5, 6]
Display the Department List ['Web Designing', 40, 20]
Display the CS Department [58, 'Ms Wiley']

讓我們討論在 Python 程序中相加兩個列表的各種方法。

方法 1:使用簡單方法相加兩個列表:

這是一個簡單的方法,在 Python 中使用循環相加兩個列表,追加方法將列表的總和相加到第三個列表中。for循環執行具有相同索引號的兩個列表的相加,並不斷迭代元素,直到列表結束。在追加方法之後,將相加的元素插入到第三個列表中。

讓我們考慮一個程序,使用天真的方法在 Python 中相加這兩個列表。

naivePro.py


# initialize the Python lists
lt1 = [5, 10, 15, 20, 25, 30]
lt2 = [2, 4, 6, 8, 10, 12]

# print the original list element
print ( " Python Original list 1: " + str (lt1))
print ( "Python Original list 2: " + str (lt2))

# use naive method to add two list.
res_lt = [] # declaration of the list
for x in range (0, len (lt1)):
    res_lt.append( lt1[x] + lt2[x])

# Display the sum of two list in Python
print ( " Addition of the list lt1 and lt2 is: " + str (res_lt))

輸出:

Python Original list 1: [5, 10, 15, 20, 25, 30]
Python Original list 2: [2, 4, 6, 8, 10, 12]
 Addition of the list lt1 and lt2 is: [7, 14, 21, 28, 35, 42]

方法 2:使用理解列表相加兩個列表

這是 Python 中天真方法的一種速記技術。理解技巧可以更快地輸入和檢索兩個列表的相加。因此,在 Python 編程中使用它來執行這些類型的任務。

讓我們考慮一個程序,使用天真的方法在 Python 中相加這兩個列表。

理解. py


# initialize the Python lists
lt1 = [2, 4, 6, 8, 10, 30]
lt2 = [2, 4, 6, 8, 10, 12]

# print the original list element
print ( " Python list 1 : " + str (lt1))
print ( "Python list 2 : " + str (lt2))

# use list comprehension to add two lists.
res_lt = [ lt1[x] + lt2[x] for x in range (len (lt1))]

# Display the sum of two list in Python
print ( " Addition of the list lt1 and lt2 is: " + str (res_lt))    

輸出:

Python list 1 : [2, 4, 6, 8, 10, 30]
Python list 2 : [2, 4, 6, 8, 10, 12]
 Addition of the list lt1 and lt2 is: [4, 8, 12, 16, 20, 42]

方法 3:使用帶有 Add 操作符的 map()函數在 Python 中相加兩個列表:

在 Python 中,map()函數用於繞過列表變數(lt1,lt2)相加兩個列表,並作為參數相加。在 map()函數中,一個相加的參數就像一個相加列表並返回總和的附加運算符。

考慮一個在 Python 中使用 map()函數和 add 運算符相加兩個列表的程序。

AddMap.py


from operator import add # import the add operator from the operator module
# initialize the lt1 and lt2 as the Python list' element
lt1 = [4, 8, 12, 16, 20, 24]
lt2 = [2, 4, 6, 8, 10, 12]

# display the original items of the lists lt1 and lt2
print ("Display the elements of List 1 " + str(lt1))
print ("Display the elements of List 2 " + str(lt2))

# use map() function with add operator to add the elements of the lists lt1 and lt2
res_lt = list( map (add, lt1, lt2)) # pass the lt1, lt2 and add as the parameters

# Display the sum of the two list
print (" Sum of the list 1 and list 2 is : " + str(res_lt))

輸出:

Display the elements of List 1 [4, 8, 12, 16, 20, 24]
Display the elements of List 2 [2, 4, 6, 8, 10, 12]
 Sum of the list 1 and list 2 is: [6, 12, 18, 24, 30, 36]

方法 4:接受來自用戶的列表元素,並連接兩個列表。

在這個程序中,我們輸入用戶的列表元素,並使用 for循環將它們插入列表中。之後,在 Python 程序中執行這兩個列表的相加。

讓我們考慮一個從用戶那裡獲取輸入列表元素並相加它們的程序。

Accept.py


# Declaration of the lt1, lt 2 and lt3 lists
lt1 = []
lt2 = []
lt3 = []

# Takes a numeric number from the user to define the total size of the list
items = int (input (" Enter the total number of the list elements: "))

# Enter the list elements from the user one by one.
print (" Enter the items into List 1 : ")
for i in range(1, items + 1):
    num = int ( input (" Enter the value of %d index is :" %i))
    lt1.append(num) # insert the items into the list1

# Enter the list elements from the user one by one.
print (" Enter the items into the List 2 : ")
for i in range(1, items + 1):
    num = int ( input (" Enter the value of %d index is :" %i))
    lt2.append(num) # insert the items into the list2

for j in range(items):
    lt3.append (lt1[j] + lt2[j]) # add the list items of both list lt1 and lt2 into the lt3   
print ("\n Addition of the two lists is ", lt3)

輸出:

Enter the total number of the list elements: 5
 Enter the items into the List 1:
 Enter the value of 1 index is: 3
 Enter the value of 2 index is: 6
 Enter the value of 3 index is: 9
 Enter the value of 4 index is: 12
 Enter the value of 5 index is: 15
 Enter the items into the List 2:
 Enter the value of 1 index is: 2
 Enter the value of 2 index is: 4
 Enter the value of 3 index is: 6
 Enter the value of 4 index is: 8
 Enter the value of 5 index is: 10

 The addition of the two list is [5, 10, 15, 20, 25]

方法 5:使用 zip()函數和 sum()函數相加兩個列表:

sum()函數用於使用由 zip()函數分組的列表元素的索引號來相加兩個列表。在 sum()函數中使用了一個 zip()函數,使用索引方式的列表對列表元素進行分組。

讓我們考慮一個使用 Python 中的 zip 函數和 sum 函數相加列表元素的程序。

zipSum.py


# initializing of the lists lt1 and lt2
lt1 = [6, 12, 18, 3, 6, 9]
lt2 = [4, 8, 12, 2, 4, 6]

# display the original items of the lists lt1 and lt2
print ("Display the elements of List 1 " + str(lt1))
print ("Display the elements of List 2 " + str(lt2))

# use the zip() function and sum() function to group the lists add the lists' lt1 and lt2 with index #wise. 
result_lt = [sum(i) for i in zip(lt1, lt2 )]

# Display the sum of the two list
print (" Sum of the list 1 and list 2 is : " + str(result_lt))

輸出:

Display the elements of List 1 [6, 12, 18, 3, 6, 9]
Display the elements of List 2 [4, 8, 12, 2, 4, 6]
 Sum of the list 1 and list 2 is : [10, 20, 30, 5, 10, 15]

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297425.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:15
下一篇 2024-12-28 12:15

相關推薦

  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python讀取CSV數據畫散點圖

    本文將從以下方面詳細闡述Python讀取CSV文件並畫出散點圖的方法: 一、CSV文件介紹 CSV(Comma-Separated Values)即逗號分隔值,是一種存儲表格數據的…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29

發表回復

登錄後才能評論