正方形数组转置行标和列标的
时间: 2023-11-17 15:02:55 浏览: 42
方形数组转置行标和列标的方法有两种,分别是按行定义和按列定义。
按行定义:行标取自然排列,列标取排列的所有可能。不同行不同列取出不同个元素相乘。符号由列标排列的逆序数的奇偶性决定。
按列定义:列标取自然排列,行标取排列的所有可能。不同行不同列取出不同个元素相乘。符号由行标排列的逆序数的奇偶性决定。
下面是按行定义和按列定义的Python代码实现:
```python
# 按行定义
def transpose_by_row(matrix):
n = len(matrix)
res = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
index = 0
for k in range(n):
if matrix[i][k] in matrix[i][:k]:
continue
for l in range(n):
if matrix[l][j] in matrix[:l][j]:
continue
if matrix[i][k] == matrix[l][j]:
index += 1
res[i][j] = (-1) ** index
return res
# 按列定义
def transpose_by_col(matrix):
n = len(matrix)
res = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
index = 0
for k in range(n):
if matrix[k][i] in matrix[:k][i]:
continue
for l in range(n):
if matrix[j][l] in matrix[j][:l]:
continue
if matrix[k][i] == matrix[j][l]:
index += 1
res[i][j] = (-1) ** index
return res
```
相关推荐
![cbp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)