Python – 将元组矩阵转换为元组列表
给定一个元组矩阵,展平为元组列表,每个元组代表每一列。
Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]]
Output : [(4, 7, 10, 18), (5, 8, 13, 17)]
Explanation : All column number elements contained together.
Input : test_list = [[(4, 5)], [(10, 13)]]
Output : [(4, 10), (5, 13)]
Explanation : All column number elements contained together.
方法 #1:使用列表理解 + zip()
在此,我们使用列表理解执行展平任务,并使用 zip() 执行列配对以呈现为元组对。
Python3
# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using list comprehension + zip()
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
# printing original list
print("The original list is : " + str(test_list))
# flattening
temp = [ele for sub in test_list for ele in sub]
# joining to form column pairs
res = list(zip(*temp))
# printing result
print("The converted tuple list : " + str(res))
Python3
# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using chain.from_iterable() + zip()
from itertools import chain
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
# printing original list
print("The original list is : " + str(test_list))
# flattening using from_iterable
res = list(zip(*chain.from_iterable(test_list)))
# printing result
print("The converted tuple list : " + str(res))
输出
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]
方法 #2:使用 chain.from_iterable() + zip()
在此,使用 chain.from_iterable() 执行展平任务,并使用 zip() 执行列配对任务。
Python3
# Python3 code to demonstrate working of
# Convert Tuple Matrix to Tuple List
# Using chain.from_iterable() + zip()
from itertools import chain
# initializing list
test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
# printing original list
print("The original list is : " + str(test_list))
# flattening using from_iterable
res = list(zip(*chain.from_iterable(test_list)))
# printing result
print("The converted tuple list : " + str(res))
输出
The original list is : [[(4, 5), (7, 8)], [(10, 13), (18, 17)], [(0, 4), (10, 1)]]
The converted tuple list : [(4, 7, 10, 18, 0, 10), (5, 8, 13, 17, 4, 1)]