[英]How to get maximum values in tuple?
m = int(input("Number of matrices: "))
rows = int(input("Enter the Number of rows : "))
column = int(input("Enter the Number of Columns: "))
p = 0
for i in range(0,m):
print("Enter the elements of Matrix:")
matrix_i = [[tuple(map(float, input().split(" "))) for i in range(column)] for i in range(rows)]
p = p + 1
print("Matrix no: ", p)
for n in matrix_i:
print(n)
res = tuple(map(lambda i, j: max(i ,j) , matrix_i, matrix_i))
print(res)
得到輸出為:
Number of matrices:
3
Enter the Number of rows :
2
Enter the Number of Columns:
2
Enter the elements of Matrix:
6 7 8
6 8 2
1 2 3
9 0 1
Matrix no: 1
[(6.0, 7.0, 8.0), (6.0, 8.0, 2.0)]
[(1.0, 2.0, 3.0), (9.0, 0.0, 1.0)]
Enter the elements of Matrix:
7 4 8
5 2 8
9 3 6
0 0 0
Matrix no: 2
[(7.0, 4.0, 8.0), (5.0, 2.0, 8.0)]
[(9.0, 3.0, 6.0), (0.0, 0.0, 0.0)]
Enter the elements of Matrix:
1 2 3
5 6 7
8 4 3
8 6 4
Matrix no: 3
[(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)]
[(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)]
([(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)], [(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)])
為什么我無法在上述程序中得到矩陣元組中的最大值? 我應該怎么做才能獲得上述程序中矩陣元組中的最大值?
假設我是否需要矩陣 1 和 2、矩陣 3 和 2、矩陣 1 和 3 以及矩陣 1、2 和 3 的元組中的最大值。如何獲取所有可能的最大矩陣的集合?
例如,我需要結果是
矩陣 1、2 和 3 的元組的最大值
IE,
[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)]
[(9.0, 4.0, 6.0), (9.0, 6.0, 4.0)]
同樣對於矩陣 1 和 2、矩陣 3 和 2、矩陣 1 和 3
1.為什么我在上面的程序中無法得到矩陣元組中的最大值?
正如@KarlKnechtel 所說, matrix_i
僅存儲最后一個矩陣,因此您不能指望使用 func max() 獲得最大值。 此外, max(tuple1, tuple2) 不會像您期望的那樣工作。 例如, max((2,4,1), (1,5,3))
將返回 (2,4,1) 而不是 (2,5,3)。 更多內容可以參考這個。
2.我應該怎么做才能獲得上述程序中矩陣元組中的最大值?
以下代碼可以滿足您的需求。 如果需要mat1 & mat2 & mat3,只需要在出現“輸入矩陣索引計算最大值:”消息時依次輸入0、1、2即可。
while True:
try:
m = int(input("Number of matrices: "))
break
except:
print('invalid input, please input again')
continue
while True:
try:
rows = int(input("Enter the Number of rows : "))
break
except:
print('invalid input, please input again')
continue
while True:
try:
column = int(input("Enter the Number of Columns: "))
break
except:
print('invalid input, please input again')
continue
p = 0
matrix =[]
for i in range(0,m):
print("Enter the elements of Matrix:")
try:
matrix.append([[tuple(map(float, input().split(" "))) for i in range(column)] for i in range(rows)])
except:
print('invalid input, please input again')
continue
p = p + 1
print("Matrix no: ", p)
for n in matrix[i]:
print(n)
def max_matrix(mat1, mat2):
res = mat1
for i in range(column):
for j in range(rows):
res[i][j] = tuple(max(mat1[i][j][k], mat2[i][j][k]) for k in range(len(mat1[0][0])))
return res
res = [[tuple(map(float, [0] * len(matrix[0][0][0]))) for i in range(column)] for i in range(rows)]
while True:
try:
i = int(input("Enter the index of matrix to compute max value:"))
if i > len(matrix) - 1:
print("out of index")
continue
if i == -1:
break
except:
print('invalid input, please input again')
continue
res = max_matrix(res, matrix[i])
print(res)
當我說您的問題不是關於將數據輸入矩陣時,我錯了,因為從某種意義上說,這是因為這是您沒有正確做的事情之一。 在您的代碼中,每個矩陣的用戶輸入的所有元素都通過覆蓋先前的值存儲在名為matrix_i
的單個矩陣中,這使得無法比較彼此中的內容。
以下代碼顯示了如何為多個矩陣輸入數據並將它們分別存儲在名為matrices
的列表中,以便可以根據需要將它們相互比較並打印出來等。
from pprint import pprint
m = int(input("Number of matrices: "))
rows = int(input("Enter the Number of rows : "))
columns = int(input("Enter the Number of Columns: "))
matrices = []
for i in range(m):
print("Enter the elements of Matrix:")
matrix_i = [[tuple(map(float, input().split(" "))) for c in range(columns)]
for r in range(rows)]
print("Matrix no: ", i+1)
for n in matrix_i:
print(n)
print()
matrices.append(matrix_i)
pprint(matrices)
然而,對於開發和測試目的,擁有一組硬編碼的值很有用,因此不必在每次運行代碼來測試數據時都手動重新輸入數據。
def tuple_max(t1, t2):
"""Return tuple comprised of the maximum values in each tuple argument."""
return tuple(map(max, tuple(zip(t1, t2))))
def matrix_max(m1, m2):
"""Return maxtrix of tuples comprised of maximum values for tuples in each row."""
rows = []
for r1, r2 in zip(m1, m2):
cols = []
for t1, t2 in zip(r1, r2):
cols.append(tuple_max(t1, t2))
rows.append(cols)
return rows
for i, matrix in enumerate(matrices, start=1):
print(f'm{i}: {matrix}')
print('----')
max12 = matrix_max(matrices[0], matrices[1])
print()
print('maximum value in tuples of matrix m1 and m2')
pprint(max12, width=40)
max23 = matrix_max(matrices[1], matrices[2])
print()
print('maximum value in tuples of matrices m2 and m3')
pprint(max23, width=40)
max13 = matrix_max(matrices[0], matrices[2])
print()
print('maximum value in tuples of matrices m1 and m3')
pprint(max13, width=40)
max123 = matrix_max(matrix_max(max12, max23), max13)
print()
print('maximum value in tuples of matrices m1, m2, and m3')
pprint(max123, width=40)
以下是使用硬編碼示例輸入打印的結果。 我認為它們表明您想要做的是完成:
m1: [[(6.0, 7.0, 8.0), (6.0, 8.0, 2.0)], [(1.0, 2.0, 3.0), (9.0, 0.0, 1.0)]]
m2: [[(7.0, 4.0, 8.0), (5.0, 2.0, 8.0)], [(9.0, 3.0, 6.0), (0.0, 0.0, 0.0)]]
m3: [[(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)], [(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)]]
----
maximum value in tuples of matrix m1 and m2
[[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)],
[(9.0, 3.0, 6.0), (9.0, 0.0, 1.0)]]
maximum value in tuples of matrices m2 and m3
[[(7.0, 4.0, 8.0), (5.0, 6.0, 8.0)],
[(9.0, 4.0, 6.0), (8.0, 6.0, 4.0)]]
maximum value in tuples of matrices m1 and m3
[[(6.0, 7.0, 8.0), (6.0, 8.0, 7.0)],
[(8.0, 4.0, 3.0), (9.0, 6.0, 4.0)]]
maximum value in tuples of matrices m1, m2, and m3
[[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)],
[(9.0, 4.0, 6.0), (9.0, 6.0, 4.0)]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.