首页 > 编程知识 正文

如何在 Python 中相加两个列表

时间:2023-11-25 15:30:11 阅读:309216 作者:EBHU

在本主题中,我们将学习如何在 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]

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。