Python map函数查找最大为1的行
在本教程中,我们将编写一个程序,该程序使用map 函数从矩阵中找到最大为1的行。
假设我们有以下矩阵。
矩阵=[[0,0,1],[1,1,1],[1,1,0]]
我们可以用不同的方式编写程序。但是,使用映射 功能,我们将遵循以下过程。
初始化矩阵。
使用映射 功能找出每行中1的数目。将它们存储在列表中。
从列表中打印最大值。
示例
## initializing the matrix
matrix = [
[0, 0, 1],
[1, 1, 1],
[1, 1, 0]
]
## function to find number of 1's in a row
def number_of_ones(row):
count = 0
for i in row:
if i is 1:
count += 1
return count
## finding the number of 1's in every row
## map returns an object which we converted into a list
ones_count = list(map(number_of_ones, matrix))
## printing the index of max number from the list
print(ones_count.index(max(ones_count)))输出结果
如果运行上述程序,将得到以下结果。
1
如果您对该程序有任何疑问,请在评论部分中提及它们。
热门推荐
10 古诗生日祝福语简短独特
11 婚庆花篮祝福语简短大气
12 今日祝福语简短10字
13 合字的祝福语简短
14 老爸生日暴富祝福语简短
15 特别的端午祝福语简短
16 送蛇的祝福语简短
17 新生祝福语简短8字
18 中秋妈妈生日祝福语简短