|
|
|
|
|
by masklinn
4820 days ago
|
|
FWIW here's a very similar Python version: max(col for col in table if col < max(table))
a fun one is this: first, second = max(permutations(table), 2)
although its semantics are slightly different (if the maximum of the table is duplicated, it'll be returned for both slots)or using heapq which notaddicted mentioned. |
|
In your example, if you replace max() with a function that prints something, you'll see it's executed for each value in 'table', which is extremely inefficient. This happens because Python can't guarantee that max() will return the same each time.
Similarly, while in a side-effect free context the runtime could, for example, slice 'table', perform the work using multiple concurrent threads and then join the result, Python has to guarantee that the execution is done sequentially, since a change in order could affect the result.
Unlike in SQL, in Python you're always telling it HOW you want it done.