使用map函数的Python程序查找最大数目为1的行
给定2D数组,数组的元素为0和1。对所有行进行排序。我们必须找到最大数目为1的行。这里我们使用map()。map函数是用于函数编程的Python内置函数中最简单的一个。这些工具将函数应用于序列和其他可迭代项。
示例
Input : Input Array is : [[0, 1, 1, 1, 1],[0, 0, 1, 1, 1],[1, 1, 1, 1, 1],[0, 0, 0, 0, 1]] The maximum number of 1's = 2
算法
Step 1: Sum of on each row of the matrix using map function. Step 2: It will return a list of sum of all one's in each row. Step 3: Then print index of maximum sum in a list.
范例程式码
# Python program to find the row with maximum number of 1's
def maximumofones(n):
max1 = list(map(sum,n))
print ("MAXIMUM NUMBER OF 1's ::>",max1.index(max(max1)))
# Driver program
if __name__ == "__main__":
n = [[0, 1, 1, 1, 1],[0, 0, 1, 1, 1],[1, 1, 1, 1, 1],[0, 0, 0, 0, 1]]
maximumofones(n)输出结果
MAXIMUM NUMBER OF 1's ::> 2
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短