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?
map()takes each element in turn from all sequences passed as the second and subsequent arguments. Therefore, the code is equivalent to:
map()
print([max(4, 1), max(3, 9), max(7, 2)])
You think about
print(list(map(max, [[4,3,7], [1,9,2]]))) # ^ ^
map, [4,3,7] [1,9,2].
map
[4,3,7]
[1,9,2]
, :
[4,3,7] [1,9,2] map. map , , max.
max
max([4, 3, 7]) max([1, 9, 2])
max(4, 1) max(3, 9) max(7, 2)
, , , map() python, . print([max(x) for x in [(4,1),(3,9),(7,2)]]).
print([max(x) for x in [(4,1),(3,9),(7,2)]])
, - .
Source: https://habr.com/ru/post/1682811/More articles:https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1682806/jlabels-tooltip-text-disabling-its-parent-jpanel-mouseenter-event&usg=ALkJrhh001kWf6tL5GLn24Ir2fn4Ho-h-QCompilationErrorException: "cannot convert from" native type when passing a value in a method in CSharpScript - c #Is there a built-in / simple method for decomposing LDU in Numpy? - pythonscipy LU factorization permutation matrix - pythonHow to get data from a form using the POST route? - javascriptIs it possible to change two functions using parforma? - functionHow to trigger a window resize event in Angular - javascriptHow to convert list list to piece (dataframe) - rWhat is the difference between microservices and web services? - web-servicesCannot access AbortButton while performing background operation - multithreadingAll Articles