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 送给姐姐简短的祝福语
13 小年好的祝福语简短
14 老婆生日祝福语简短孩子
15 年底老板祝福语简短的
16 给老人的祝福语简短
17 开业祝福语简短大气的
18 步入高中祝福语简短励志