Python - 矩阵中的垂直连接

2024 年 8 月 29 日 | 4 分钟阅读

引言

在本教程中,我们将学习 Python 中的矩阵垂直连接。它接受矩阵形式的输入,可以执行按列的字符串连接。它还处理列表变量的长度。当您想垂直组合矩阵时,可以使用列表推导式。

示例

现在,我们给出了一些矩阵垂直连接的示例。

给出 Python 中矩阵垂直连接的一些示例

现在,我们给出 Python 中矩阵垂直连接的一些示例。示例如下 -

程序代码 1

在这里,我们给出了一个使用循环在矩阵中进行垂直连接的示例。代码是用 Python 编写的,如下所示 -

输出

现在我们在 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 编写的,如下所示 -

输出

现在我们在 Python 中编译上述代码,并在成功编译后运行它。然后输出如下:

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 编写的,如下所示 -

输出

现在我们在 Python 中编译上述代码,并在成功编译后运行它。然后输出如下:

The list of the string is: [['Hello', 'coders'], ['welcome', 'to'], ['JavaTpoint']]
The list of the string after column wise Concatenation is: ['HellowelcomeJavaTpoint', 'codersto']