列表数组中的最大值

时间:2016-11-21 20:36:24

标签: python-3.x max

以下代码创建一个包含输入值的列表:

def locateLargest():

    matrix = []
    numberOfRows = int(input("Enter the number of rows: "))
    numberOfColumns = 2

    for row in range(0, numberOfRows):
        matrix.append([])
        for column in range(0, numberOfColumns):
            value = int(input("Enter a value: "))
            matrix[row].append(value)


    max_value = None
    for value in matrix:
        if not max_value:
            max_value = value
        elif value > max_value:
            max_value = value
    print(max_value)

locateLargest()

我遇到的问题是它要求行中的每个值,并且返回行中的最大值对,而不是最大值的索引。

我应该得到的样本是:

Enter the number of rows in the list: 3
Enter a row: 23.5 35 2 10
Enter a row: 4.5 3 45 3.5
Enter a row: 35 44 5.5 11.6
The location of the largest element is at (1,2)

有什么想法吗?

我目前的输出是:

Enter the number of rows: 2
Enter the number of columns: 6
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 2
Enter a value: 7
Enter a value: 6
Enter a value: 4
Enter a value: 3
Enter a value: 6
Enter a value: 2
[7, 6, 4, 3, 6, 2]

2 个答案:

答案 0 :(得分:1)

这不是'pythonic',但会帮助您实现最终目标,并希望了解这一过程。正如Łukasz所提到的,你需要为每一行和每行中的每一列做一次迭代:

首先声明变量以存储您的位置:

maxPoint = [0,0]

然后枚举您的矩阵,以便您可以从每一行获取列表,还可以检索当前活动行的索引:

for idx, row in enumerate(matrix):

在当前值列表中查找最大值,即:[10, 20, 30]

    maxRowValue = max(row)

查找此最大值所在的列,即:[0, 1, 2, ...]

    maxRowIndex = row.index(maxRowValue)

确定最大行值是否实际上大于任何其他先前定位的点,如果它更少丢弃它:

    if maxRowValue <= matrix[maxPoint[0]][maxPoint[1]]:
            continue

如果值更大,请将其保存到maxPoint变量:

    maxPoint = [idx, maxRowIndex]

修改

为了完整起见,这里添加了AChampion性能改进的完整代码示例:

def locateLargest():

    matrix = []
    numberOfRows = int(input("Enter the number of rows: "))
    numberOfColumns = 2

    for row in range(0, numberOfRows):
        matrix.append([])
        for column in range(0, numberOfColumns):
            value = int(input("Enter a value: "))
            matrix[row].append(value)

    maxPoint = [0,0]

    for rIndex, row in enumerate(matrix):
        cIndex, maxRowValue = max(enumerate(row), key=lambda x: x[1])
        if maxRowValue <= matrix[maxPoint[0]][maxPoint[1]]:
            continue
        maxPoint = [rIndex, cIndex]

    print(maxPoint)

locateLargest()

编辑2

以下是不使用枚举的相同算法:

currentRow = 0

for row in matrix:
    maxRowValue = max(row)
    maxRowIndex = row.index(maxRowValue)
    if maxRowValue > matrix[maxPoint[0]][maxPoint[1]]:
            maxPoint = [currentRow, maxRowIndex]
    currentRow += 1

答案 1 :(得分:0)

使用enumerate()和一些生成器表达式,可以减少这段代码:

  • 生成行
  • 生成每行的最大值
  • 查找所有行的最大值

可能比某些人想要的更复杂:

numberOfRows = int(input("Enter the number of rows: "))
# Generate the rows
rows = (map(int, input("Enter a row: ").split()) for _ in range(numberOfRows))

# Generate the maximum for each row
max_row = (max(enumerate(data), key=lambda x: x[1]) for data in rows)

# Find the maximum across all rows
i, (j, v) = max(enumerate(max_row), key=lambda x: x[1][1])
print("The location of the largest element is at {} [{}]".format((i, j), v))

输入/输出:

Enter the number of rows: 3
Enter a row: 1 2 3
Enter a row: 3 6 3
Enter a row: 1 2 3
'The location of the largest element is at (1, 1) [6]'

如果你想查看发生了什么,请将生成器更改为列出理解:

>>> rows = [list(map(int, input("Enter a row: ").split())) for _ in range(numberOfRows)]
Enter a row: 1 2 3
Enter a row: 3 6 3
Enter a row: 1 2 3
>>> rows
[[1, 2, 3], [3, 6, 3], [1, 2, 3]]
>>> max_row = [max(enumerate(data), key=lambda x: x[1]) for data in rows]
>>> max_row
[(2, 3), (1, 6), (2, 3)]
>>> list(enumerate(max_row))
[(0, (2, 3), (1, (1, 6)), (2, (2, 3))]
              ^^^^^^^^^
              i, (j, v)