Python syntax for calling a map (max ())

I came across this particular piece of code in one of the "beginner" tutorials for Python. This is not logical sense, if someone can explain this to me, I would appreciate it.

print(list(map(max, [4,3,7], [1,9,2])))

I thought it would print [4.9] (by running max () in each of the provided lists and then printing the maximum value in each list). Instead, he prints [4,9,7]. Why three numbers?

+4
source share
3 answers

map()takes each element in turn from all sequences passed as the second and subsequent arguments. Therefore, the code is equivalent to:

print([max(4, 1), max(3, 9), max(7, 2)])
+4
source

You think about

print(list(map(max, [[4,3,7], [1,9,2]])))
#                   ^                ^

map, [4,3,7] [1,9,2].

, :

print(list(map(max, [4,3,7], [1,9,2])))

[4,3,7] [1,9,2] map. map , , max.

max([4, 3, 7])
max([1, 9, 2])

max(4, 1)
max(3, 9)
max(7, 2)
+5

, , , map() python, . print([max(x) for x in [(4,1),(3,9),(7,2)]]).

, - .

0

Source: https://habr.com/ru/post/1682811/


All Articles