Python程序连接跨列表的每个元素
在本文中,我们将学习一个 Python 程序来连接跨列表的每个元素。
使用的方法
以下是完成此任务的各种方法 -
使用列表推导式和字符串连接
使用 itertools.product() 函数
示例
假设我们已经获取了两个输入列表。我们现在将连接这两个给定列表中的每个元素
输入
inputList1 = ["hello", "tutorialspoint", "python"] inputList2 = ["coding", "articles"]
输出
Resultant paired combinations list: ['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']
方法 1:使用列表推导式和字符串连接
列表推导式
当您希望基于现有列表的值构建新列表时,列表推导式提供了一种更短/简洁的语法。
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -。
创建一个变量来存储**输入**列表 1 并打印给定的第一个列表。
创建另一个变量来存储**输入**列表 2 并打印给定的第二个列表。
使用列表推导式遍历第一个列表和第二个列表(嵌套循环)。
在其中,使用来自第一个列表的第一个元素和来自第二个列表的第二个元素创建元组对。
通过迭代对列表,使用字符串连接连接对元素。
打印结果配对组合列表
示例
以下程序使用列表推导式和字符串连接返回跨列表每个元素的连接 -
# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]
# input list 2
inputList2 = ["coding", "articles"]
# printing input list 1
print("Input List 1:", inputList1)
# printing input list 2
print("Input List 2:", inputList2)
# Using a list to traverse the first list and the second list
# Creating tuple pairs with the first element from the first list
# and the second element from the second list
pairs_list = [(p, q) for p in inputList1 for q in inputList2]
# Iterating in the above pairs list and
# concatenating the tuples(string concatenation)
resultantList = [m + ' ' + n for (m, n) in pairs_list]
# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)
输出
执行后,上述程序将生成以下输出 -
Input List 1: ['hello', 'tutorialspoint', 'python'] Input List 2: ['coding', 'articles'] Resultant paired combinations list: ['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']
**时间复杂度** - O(n^2),因为有两个 for 循环
**空间复杂度** - O(n)
方法 2:使用 itertools.product() 函数
**Itertools.product()** 返回笛卡尔积。
让我们看看什么是**笛卡尔积** -
在数学方面,所有有序对 (a, b) 的集合(其中 a 属于 A,b 属于 B)被称为两个集合的笛卡尔积。请查看下面的示例以更好地理解。
示例
list 1= [10, 20, 30] list2 = [2, 4, 6]
输出
[(10, 2), (10, 4), (10, 6), (20, 2), (20, 4), (20, 6), (30, 2), (30, 4), (30, 6)]
算法(步骤)
以下是执行所需任务需要遵循的算法/步骤 -
使用 import 关键字从 itertools 模块导入**product()** 函数。
创建一个变量来存储**输入**列表 1 并打印给定的第一个列表。
创建另一个变量来存储**输入**列表 2 并打印给定的第二个列表。
获取一个空列表来存储结果。
使用 product() 函数遍历给定列表的笛卡尔积。
使用 + 运算符(字符串连接)连接两个列表元素。
使用 append() 函数将此对字符串添加到结果中。
打印结果。
示例
以下程序使用 itertools.product() 和字符串连接返回跨列表每个元素的连接 -
# importing product from itertools module
from itertools import product
# input list 1
inputList1 = ["hello", "tutorialspoint", "python"]
# input list 2
inputList2 = ["coding", "articles"]
# printing input list 1
print("Input List 1:", inputList1)
# printing input list 2
print("Input List 2:", inputList2)
# Taking an empty list to store the concatenation result
resultantList = []
# Iterating the cartesian product of the two lists
for e in product(inputList1, inputList2):
# Concatenating two lists elements with space as a separator
pairString = e[0]+' '+e[1]
# Adding this pair string to the result list at the end of the result list
resultantList.append(pairString)
# printing resultant paired combinations list
print("Resultant paired combinations list:\n", resultantList)
输出
执行后,上述程序将生成以下输出 -
Input List 1: ['hello', 'tutorialspoint', 'python'] Input List 2: ['coding', 'articles'] Resultant paired combinations list: ['hello coding', 'hello articles', 'tutorialspoint coding', 'tutorialspoint articles', 'python coding', 'python articles']
结论
在本文中,我们学习了如何使用两种不同的方法连接给定的两个列表中的每个元素。此外,我们了解了笛卡尔积是什么,如何使用 product() 方法计算它以及示例。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP