python why does max(max(float_2d_array)) give wrong answer?

floating-point, matrix, max, python

Solution

Lists are sorted element-wise. Since the index of `1.2852976787772832` is one place ahead of that of `6.409872844109646` in the candidate sublists, the list containing the former gets picked as the maximum.

In the same index in the second list, we have a `0` and `1.2852976787772832` is clearly greater than 0:

[0.0, 0.0, 1.2852976787772832, 0.7965388321000092, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 6.409872844109646, 0.17506688391255013, 0.0, 0.0, 0.0, 0.0]
#           ^ here's your tie-breaker 

In fact, the next index containing `6.4...` is never checked.

I'm not sure how you expect the maximum sublist to be selected: sublist with maximum sum, sublist containing maximum number? You'll have to code the behavior you want if the default behavior does not cut it.

Problem

for example: ``` a = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.2852976787772832, 0.7965388321000092, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 6.409872844109646, 0.17506688391255013, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] print max(max(a)) print max(a) ``` The result is: 1.28529767878 [0.0, 0.0, 1.2852976787772832, 0.7965388321000092, 0.0, 0.0, 0.0, 0.0, 0.0] This is clearly wrong, the max value should be 6.409872844109646. ``` b = [] for i in a: b.extend(i) print max(b) ``` 6.40987284411 This is python 2.7, Cpython. Thank you very much.

Original source