在本教程中,我们将学习 Python - 矩阵中的垂直串联。它将输入作为矩阵形式,可以按列执行字符串连接。它还处理列表变量的长度。当您想要垂直组合矩阵时,可以使用列表理解。
例子:
现在,我们给出了矩阵中垂直串联的一些示例。
输入: [["Hello"], ["good"]], ["morning"]]
输出: ['Hellogoodmorning']
说明:对给定的输入字符串矩阵按列进行连接。这里,"Hello "与 "good "和 "morning "连接;
输入: [["Hello", "everyone"], ["good", "morning"]]
输出: ['Hellogood', 'everyonemorning']
说明:给定的输入字符串矩阵是按列连接的。这里,"Hello "与 "good "连接,"everyone "与 "morning "连接;
输入: [["Hello", "every", "one"], ["very", "good", "morning"]]
输出: ['Hellovery', 'everygood', "onemorning"]
说明:给定的输入字符串矩阵是按列连接的。这里,"Hello "与 "very "连接,"every "与 "good "连接,"one "与 "morning "连接。
举一些Python中矩阵垂直串联的例子:
现在,我们给出一些Python中矩阵垂直串联的例子。示例如下 -
程序代码1:
在这里,我们给出了使用循环在矩阵中垂直串联的示例。代码是用 Python 编写的,如下所示 -
使用循环在矩阵中进行垂直连接的示例 # 初始化字符串矩阵列表 ; list = [["Hello", "everyone"], ["good", "morning"], ["Kolkata"]] # 打印给定字符串的列表; print("The list of the string is : " + str(list)) # 在此处使用执行迭代的循环 ; res = [] n = 0 while n != len(list): temp = '' for idx in list: # 此处正在检查有效列 ......; try: temp = temp + idx[n] except IndexError: pass res.append(temp) n = n + 1 res = [ele for ele in res if ele] # Print the final output of the list here print("The list of the string after column wise Concatenation is: " + str(res))
|
输出:
现在我们用 Python 编译上述代码,编译成功后运行它。输出结果如下
The list of the string is: [['Hello', 'everyone'], ['good', 'morning'], ['Kolkata']] The list of the string after column wise Concatenation is: ['HellogoodKolkata', 'everyonemorning']
|
程序代码 2:
现在,我们再举一个使用软件包在矩阵中进行垂直连接的例子。我们使用 zip_longest 和 join() 函数对列表进行垂直连接。在这里,我们在列表中给出了三列,从而执行了按列连接。代码用 Python 编写,如下所示
# 在此导入 zip_longest 库 ; from itertools import zip_longest # 初始化字符串矩阵 ; list = [["Hello," "coders"], ["welcome", "to"], ["JavaTpoint"]] # Print the list of the given string print("The list of string is: ", list) 使用 zip_longest 和 join() 函数查找结果; res = ["".join(elem) for elem in zip_longest(*list, fillvalue ="")] # Print the final output of the list here print("The list of the string after column wise Concatenation is: ", res)
|
输出:
The list of the string is: [['Hello', 'coders'], ['welcome', 'to'], ['JavaTpoint']]
The list of the string after column wise Concatenation is: ['HellowelcomeJavaTpoint', 'codersto']
程序代码 3:
我们给出最后一个使用 numpy 库在矩阵中进行按列或垂直连接的示例。在这里,我们使用 numpy.transpose() 和 numpy.ravel() 函数来执行列式连接。代码用 Python 编写,如下所示
# Import numpy library import numpy as np # Initialize the list of string matrix list = [["Hello", "coders"], ["welcome", "to"], ["JavaTpoint"]] # Print the list of the given string print("The list of the string is: ", list) 找出最大长度 ; maxlen = max(len(sublist) for sublist in list) # 用空字符串填充子列表,使每个子列表的长度相同 ; p_list = [sublist + [''] * (maxlen - len(sublist)) for sublist in list] # Here, the list id converts into a numpy array arr = np.array(p_list) 要切换行和列,请在此处使用 transpose ; arr_t = arr.T 使用 join() 函数进行行连接 ; result = [''.join(row) for row in arr_t] # 在此打印列表的最终输出 ; print("The list of the string after column wise Concatenation is: ", result)
|
输出:
The list of the string is: [['Hello', 'coders'], ['welcome', 'to'], ['JavaTpoint']]
The list of the string after column wise Concatenation is: ['HellowelcomeJavaTpoint', 'codersto']