4 回答
TA贡献1829条经验 获得超6个赞
您可以使用列表理解来应用于min每一列数据:
import random
x =[[random.randint(1,10) for j in range(5)] for i in range(4)]
for i in range(4):
for j in range(5):
print("%4d" % (x[i][j]), end="")
print(end='\n')
print([min(x[i][j] for i in range(4)) for j in range(5)])
示例输出:
8 1 7 6 9
9 2 8 6 8
10 5 3 3 5
1 9 7 10 9
[1, 1, 3, 3, 5]
TA贡献1891条经验 获得超3个赞
您应该查看 numpy() ,特别是np.min()。然后代码可以是:
# Convert 'x' to np array-
x_np = np.asarray(x)
# Sanity check-
x_np
'''
array([[ 8, 7, 10, 8, 8],
[ 6, 6, 3, 6, 6],
[ 9, 4, 2, 8, 3],
[ 2, 4, 5, 3, 4]])
'''
# Minimum elements by squashing the columns-
np.min(x_np, axis = 1)
# array([7, 3, 2, 2])
# Minimum elements by squashing the rows-
np.min(x_np, axis = 0)
# array([2, 4, 2, 3, 3])
添加回答
举报