Python程序通过切片行和列总和对矩阵进行排序
给定一个矩阵和一系列索引,任务是编写一个Python程序,该程序可以根据每行和列的给定索引范围的总和对矩阵进行排序,即行和列要从给定的开始到结束索引,此外,矩阵仅使用来自每行或每列的那些切片总和进行排序。
Input : test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]], i, j = 1, 3
Output : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]]
Explanation : 7 < 8 < 9 < 10, is summation of 1st and 2nd element.
Input : test_list = [[1, 4, 3, 1, 3], [23, 5, 5, 3], [2, 3, 5, 1, 6]], i, j = 1, 3
Output : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [23, 5, 5, 3]]
Explanation : 7 < 8 < 10, is summation of 1st and 2nd element.
行求和
方法 1:使用sort() 、 slice和sum()
在这里,我们使用 sort() 执行就地排序的任务,使用 sum() 完成求和,使用列表切片完成切片操作,它们一起形成排序的关键函数。
例子:
Python3
# get sliced summation
def get_sliced_sum(row):
return sum(row[i:j])
# initializing list
test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4],
[23, 5, 5, 3], [2, 3, 5, 1, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# performing sort
test_list.sort(key=get_sliced_sum)
# printing result
print("Sorted List : " + str(test_list))
Python3
# initializing list
test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4],
[23, 5, 5, 3], [2, 3, 5, 1, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
# printing result
print("Sorted List : " + str(res))
Python3
# get sliced summation
def get_sliced_sum(row):
return sum(row[i:j])
# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2],
[23, 5, 5, 3], [2, 3, 5, 1]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# transposing matrix
test_list = list(zip(*test_list))
# performing sort
test_list.sort(key=get_sliced_sum)
# performing transpose again to get
# result.
test_list = zip(*test_list)
# converting list of tuples to list of
# lists
res = [list(sub) for sub in test_list]
# printing result
print("Sorted List Columnwise : " + str(res))
Python3
# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2],
[23, 5, 5, 3], [2, 3, 5, 1]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# transposing matrix
test_list = zip(*test_list)
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
# performing transpose again to get result.
res = zip(*res)
# converting list of tuples to list of lists
res = [list(sub) for sub in res]
# printing result
print("Sorted List Columnwise : " + str(list(res)))
输出:
The original list is : [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]]
Sorted List : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]]
方法 2:使用sorted() 、 lambda 、 sum()和切片
在这种情况下,执行排序的任务是使用 sorted() 完成的,并且 lambda函数用于获取使用单语句渲染的切片的总和,无需外部函数调用。
例子:
蟒蛇3
# initializing list
test_list = [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4],
[23, 5, 5, 3], [2, 3, 5, 1, 6]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
# printing result
print("Sorted List : " + str(res))
输出:
The original list is : [[1, 4, 3, 1, 3], [3, 4, 5, 2, 4], [23, 5, 5, 3], [2, 3, 5, 1, 6]]
Sorted List : [[1, 4, 3, 1, 3], [2, 3, 5, 1, 6], [3, 4, 5, 2, 4], [23, 5, 5, 3]]
列求和
方法 1:使用sort() 、 slice和sum()
在此,我们执行基矩阵的转置,然后使用上述方法 1 执行获取切片的常规任务,排序后,矩阵再次转换为其转置格式。
例子:
蟒蛇3
# get sliced summation
def get_sliced_sum(row):
return sum(row[i:j])
# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2],
[23, 5, 5, 3], [2, 3, 5, 1]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# transposing matrix
test_list = list(zip(*test_list))
# performing sort
test_list.sort(key=get_sliced_sum)
# performing transpose again to get
# result.
test_list = zip(*test_list)
# converting list of tuples to list of
# lists
res = [list(sub) for sub in test_list]
# printing result
print("Sorted List Columnwise : " + str(res))
输出:
The original list is : [[1, 4, 3, 1], [3, 4, 5, 2], [23, 5, 5, 3], [2, 3, 5, 1]]
Sorted List Columnwise : [[1, 4, 3, 1], [2, 4, 5, 3], [3, 5, 5, 23], [1, 3, 5, 2]]
方法 2:使用sorted() 、 lambda 、 sum()和切片
在此,我们执行基矩阵的转置,然后使用上述方法 2 执行获取切片的常规任务,排序后,矩阵再次转换为其转置格式。
例子:
蟒蛇3
# initializing list
test_list = [[1, 4, 3, 1], [3, 4, 5, 2],
[23, 5, 5, 3], [2, 3, 5, 1]]
# printing original list
print("The original list is : " + str(test_list))
# initializing range
i, j = 1, 3
# transposing matrix
test_list = zip(*test_list)
# performing sort using sorted()
# filter util. using lambda fnc.
res = sorted(test_list, key=lambda row: sum(row[i:j]))
# performing transpose again to get result.
res = zip(*res)
# converting list of tuples to list of lists
res = [list(sub) for sub in res]
# printing result
print("Sorted List Columnwise : " + str(list(res)))
输出:
The original list is : [[1, 4, 3, 1], [3, 4, 5, 2], [23, 5, 5, 3], [2, 3, 5, 1]]
Sorted List Columnwise : [[1, 4, 3, 1], [2, 4, 5, 3], [3, 5, 5, 23], [1, 3, 5, 2]]