Python 中矩阵的垂直连接


需要垂直连接矩阵时,可以使用列表解析。

以下是演示 −

示例

 在线演示

from itertools import zip_longest

my_list = [["Hi", "Rob"], ["how", "are"], ["you"]]

print("The list is : ")
print(my_list)

my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ="")]

print("The list after concatenating the column is : ")
print(my_result)

输出

The list is :
[['Hi', 'Rob'], ['how', 'are'], ['you']]
The list after concatenating the column is :
['Hihowyou', 'Robare']

说明

  • 导入所需的包。

  • 定义一个列表列表,并在控制台上显示。

  • 列表解析用于压缩元素,并通过消除空格连接它们。

  • 这被赋给一个变量。

  • 此变量在控制台上显示为输出。

更新于: 2021-04-16

343 次浏览

开启你的 职业生涯

完成课程后获得认证

开始
广告
© . All rights reserved.