在本主題中,我們將學習如何在 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-hant/n/297425.html