在这个话题中,我们将学习如何在Python中添加两个列表。但在深入研究这个话题之前,我们需要理解Python中的列表(List)这个术语。Python列表用于在一个变量中存储多个项目。列表中的项目可以是任何有序、可更改的项目,并允许存储重复值。列表的每个项目都有一个适当的索引值,其中列表的第一个索引从{0}开始,列表的长度索引必须是n-1。列表的每个项目都用逗号(,)符号分隔,并封装在方括号[]中。

162.png

语法

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

这里L1和L2是两个包含相同或不同数据类型元素的列表。列表L1包含intstring数据类型的元素,而列表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:使用朴素方法添加两个列表:

这是一种简单的方法,使用循环附加方法将两个列表添加到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中添加两个列表的程序。

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中,使用map()函数通过传递列表变量(lt1、lt2)并将其作为参数来添加两个列表,作为参数的add参数充当附加列表并返回和。

考虑一个使用map()函数和add运算符在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]

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程