Python程序按索引值相等计数排序矩阵
给定一个矩阵,任务是编写一个Python程序,该程序可以根据等于其索引号的值数量的度量对其行或列进行排序。对于每一行或每一列,计算索引号与值相等的出现次数。在计算每行或每列的计数后,根据在步骤 1 中为每行或每列提取的计数对矩阵进行排序。
Input : test_list = [[3, 1, 2, 5, 4], [0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Output : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [3, 1, 2, 5, 4], [0, 1, 2, 3, 4]]
Explanation : 0 < 2 < 3 < 5 is ordering for count of index-element equality. 2 is equal to its index position in 2nd list.
Input : test_list = [[0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Output : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [0, 1, 2, 3, 4]]
Explanation : 0 < 2 < 5 is ordering for count of index-element equality. All elements in last list are equal to its index hence the returned count.
排序行
方法 1:使用sort() 、 len()和enumerate()
在这里,我们通过比较枚举组件来检查索引和元素是否相等,结果的总和是通过提取列表的长度来完成的。这作为执行就地排序的 sort() 的键传递。
例子:
Python3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sort() + len() + enumerate()
def get_idx_ele_count(row):
# getting required count
# element and index compared, if equal added
# in list, length computed using len()
return len([ele for idx, ele in enumerate(row) if ele == idx])
# initializing list
test_list = [[3, 1, 2, 5, 4], [0, 1, 2, 3, 4],
[6, 5, 4, 3, 2], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# inplace sorting using sort()
test_list.sort(key=get_idx_ele_count)
# printing result
print("Sorted List : " + str(test_list))
Python3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sorted() + lambda + len() + enumerate()
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# sorting using sorted()
# utility injection using lambda
# element and index compared, if equal added in list,
# length computed using len()
res = sorted(test_list, key=lambda row: len(
[ele for idx, ele in enumerate(row) if ele == idx]))
# printing result
print("Sorted List : " + str(res))
Python3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sort() + len() + enumerate()
def get_idx_ele_count(row):
# getting required count
# element and index compared, if equal added in
# list, length computed using len()
return len([ele for idx, ele in enumerate(row) if ele == idx])
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# transposing matrix
test_list = list(zip(*test_list))
# inplace sorting using sort()
test_list.sort(key=get_idx_ele_count)
# 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 : " + str(res))
Python3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sorted() + lambda + len() + enumerate()
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# transposing matrix
test_list = zip(*test_list)
# sorting using sorted()
# utility injection using lambda
# element and index compared, if equal added in
# list, length computed using len()
res = sorted(test_list, key=lambda row: len(
[ele for idx, ele in enumerate(row) if ele == idx]))
# 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 : " + str(res))
输出:
The original list is : [[3, 1, 2, 5, 4], [0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Sorted List : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [3, 1, 2, 5, 4], [0, 1, 2, 3, 4]]
方法 2:使用sorted() 、 lambda 、 len()和enumerate()
在这里,我们使用 sorted() 执行排序任务,而 lambda函数提供了与 len() 和 enumerate() 结合使用进行排序的实用程序。
例子:
蟒蛇3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sorted() + lambda + len() + enumerate()
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# sorting using sorted()
# utility injection using lambda
# element and index compared, if equal added in list,
# length computed using len()
res = sorted(test_list, key=lambda row: len(
[ele for idx, ele in enumerate(row) if ele == idx]))
# printing result
print("Sorted List : " + str(res))
输出:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[6, 5, 4, 3], [0, 5, 4, 2], [3, 1, 2, 5], [0, 1, 2, 3]]
排序列
方法 1:使用sort() 、 len()和enumerate()
在此,我们执行基矩阵的转置,然后使用上述方法 1 执行获取索引值相等计数的常规任务,排序后,矩阵再次转换为其转置格式。
例子:
蟒蛇3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sort() + len() + enumerate()
def get_idx_ele_count(row):
# getting required count
# element and index compared, if equal added in
# list, length computed using len()
return len([ele for idx, ele in enumerate(row) if ele == idx])
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# transposing matrix
test_list = list(zip(*test_list))
# inplace sorting using sort()
test_list.sort(key=get_idx_ele_count)
# 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 : " + str(res))
输出:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[3, 2, 5, 1], [0, 2, 3, 1], [6, 4, 3, 5], [0, 4, 2, 5]]
方法 2:使用sorted() 、 lambda 、 len()和enumerate()
在这里,我们执行基矩阵的转置,然后使用上述方法2执行获取索引值相等计数的常规任务,排序后,矩阵再次转换为其转置格式。
例子:
蟒蛇3
# Python3 code to demonstrate working of
# Sort Matrix by index-value equality count
# Using sorted() + lambda + len() + enumerate()
# initializing list
test_list = [[3, 1, 2, 5], [0, 1, 2, 3],
[6, 5, 4, 3], [0, 5, 4, 2]]
# printing original list
print("The original list is : " + str(test_list))
# transposing matrix
test_list = zip(*test_list)
# sorting using sorted()
# utility injection using lambda
# element and index compared, if equal added in
# list, length computed using len()
res = sorted(test_list, key=lambda row: len(
[ele for idx, ele in enumerate(row) if ele == idx]))
# 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 : " + str(res))
输出:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[3, 2, 5, 1], [0, 2, 3, 1], [6, 4, 3, 5], [0, 4, 2, 5]]