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']
说明
导入所需的包。
定义一个列表列表,并在控制台上显示。
列表解析用于压缩元素,并通过消除空格连接它们。
这被赋给一个变量。
此变量在控制台上显示为输出。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP