如何在Python中添加两个列表
在这个主题中,我们将学习如何在Python中添加两个列表。但在讨论这个主题之前,我们需要了解Python中的 列表 一词。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 和 string 数据类型的元素,而列表L2只包含 int 数据类型的元素。
让我们考虑一个在Python中打印列表的程序。
programList.py
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:使用朴素方法添加两个列表
这是一种简单的方法,它使用 循环 和 追加 方法将两个列表相加,将列表的总和添加到第三个列表中。一个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程序。
comprehension.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:使用map()函数和加法运算符在Python中添加两个列表
在Python中,使用map()函数可以通过将列表变量(lt1、lt2)传递给它作为参数来添加两个列表。在map()函数内部,一个额外的参数就像一个附加的运算符一样,用来将两个列表相加并返回它们的和。
考虑一个程序,使用map()函数和加法运算符在Python中添加两个列表。
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()函数来按索引分组列表。
让我们考虑一个使用zip函数和sum函数在Python中添加列表元素的程序。
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]