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