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.