+ 1
Explain the map() fn in creating array
5 Respuestas
+ 3
The map() function applies a function to an iterable of numbers (e.g. a list).
Let's say you have a simple function that multiplies a number by two:
def multiply_by_two(number):
return number * 2
and you want to apply it to a a list of numbers:
numbers = [1, 2, 3, 4, 5]
you can use the map() function like this:
numbers_multiplied_by_two = map(multiply_by_two, numbers)
This will return a map object:
print(type(numbers_multiplied_by_two))
# output: <class 'map'>
To convert it to a list, use list():
print(list(numbers_multiplied_by_two))
# output: [2, 4, 6, 8, 10]
(You can get the same result in a more compact way with print(list(map(lambda n: n*2, list(range(1, 6)))))).
+ 2
input().split() will split the input (which is a string) into a list of strings. Without an argument, whitespace is used to split the string:
input_string = '1 3 5 89 48'
input_string.split() = ['1', '3', '5', '89', '48']
map(int, input_string) applies the int() function to every item of that list.
So you'll end up with [1, 3, 5, 89, 48] (each item of that list is an integer, not a string). Note that this will result in a ValueError in case some of the elements of the list can't be converted to integers.
+ 2
Thank you very much😃
+ 1
A= list(map(int,input().Split()))..
.what is the meaning for this line
0
You're welcome 😀