Python - 测试连续元素矩阵
给定一个矩阵,测试它是否由连续元素组成。
Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Output : True
Explanation : Elements in Matrix range from 4 to 12.
Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]]
Output : False
Explanation : Elements not consecutive and not in a range.
方法#1:使用循环
在这里,我们检查行内的连续性,并在每行之前,检查第一个元素是否与上一行的最后一个元素相邻。
Python3
# Python3 code to demonstrate working of
# Test Consecutive Element Matrix
# Using loop
# initializing list
test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
# printing original list
print("The original list is : " + str(test_list))
res = True
mem_list = []
for sub in test_list:
flag = True
for ele in sub:
# checking for last element to be Consecutive
if len(mem_list) != 0:
if mem_list[-1] != ele - 1:
flag = False
break
mem_list.append(ele)
if not flag:
res = False
break
# printing result
print("Is Matrix Consecutive ? : " + str(res))
Python3
# Python3 code to demonstrate working of
# Test Consecutive Element Matrix
# Using chain.from_iterable() + all()
from itertools import chain
# initializing list
test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
# printing original list
print("The original list is : " + str(test_list))
# flattening matrix
test_list = list(chain.from_iterable(test_list))
# checking for boolean true
res = all(test_list[idx - 1] == test_list[idx] -
1 for idx in range(1, len(test_list)))
# printing result
print("Is Matrix Consecutive ? : " + str(res))
输出
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True
方法#2:使用chain.from_iterable() + all()
在这里,我们将矩阵展平到列表,然后使用 all() 检查所有连续的元素,是否比展平后的 Matrix 中的前一个元素大 1。
蟒蛇3
# Python3 code to demonstrate working of
# Test Consecutive Element Matrix
# Using chain.from_iterable() + all()
from itertools import chain
# initializing list
test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
# printing original list
print("The original list is : " + str(test_list))
# flattening matrix
test_list = list(chain.from_iterable(test_list))
# checking for boolean true
res = all(test_list[idx - 1] == test_list[idx] -
1 for idx in range(1, len(test_list)))
# printing result
print("Is Matrix Consecutive ? : " + str(res))
输出
The original list is : [[4, 5, 6], [7], [8, 9, 10], [11, 12]]
Is Matrix Consecutive ? : True