Python – 列出矩阵中的元素分组
给定一个矩阵,根据列表元素进行分组,即每个组应包含列表中的所有元素。
Input : test_list = [[2, 6], [7, 8], [1, 4]], check_list = [1, 2, 4, 6]
Output : [[7, 8], [[1, 2], [4, 6]]]
Explanation : 1, 2, 4, 6 elements rows are grouped.
Input : test_list = [[2, 7], [7, 8], [1, 4]], check_list = [1, 2, 4, 6]
Output : [[2, 7], [7, 8], [1, 4]]
Explanation : No grouping possible.
方法:使用循环 + 列表理解 + Try-Except
在此,对于矩阵中的每一行,从列表中获取缺失的元素,获取元素后,如果我们找到缺失的元素,则与每一行匹配,如果找到,则建立新的组。
Python3
# Python3 code to demonstrate working of
# List Elements Grouping in Matrix
# Using loop
# initializing list
test_list = [[4, 6], [1, 2], [2, 6], [7, 8], [1, 4]]
# printing original list
print("The original list is : " + str(test_list))
# initializing check_list
check_list = [1, 2, 4, 6]
res = []
while test_list:
# getting row
sub1 = test_list.pop()
# getting elements not in row
sub2 = [ele for ele in check_list if ele not in sub1]
try:
# testing if we have list of removed elements
test_list.remove(sub2)
# grouping if present
res.append([sub1, sub2])
except ValueError:
# ungrouped.
res.append(sub1)
# printing result
print("The Grouped rows : " + str(res))
输出
The original list is : [[4, 6], [1, 2], [2, 6], [7, 8], [1, 4]]
The Grouped rows : [[[1, 4], [2, 6]], [7, 8], [[1, 2], [4, 6]]]