+ 1

Explain the map() fn in creating array

29th Dec 2018, 11:14 AM
Nandhini Manickam
Nandhini Manickam - avatar
5 ответов
+ 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)))))).
29th Dec 2018, 11:27 AM
Anna
Anna - avatar
+ 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.
29th Dec 2018, 11:37 AM
Anna
Anna - avatar
+ 2
Thank you very much😃
29th Dec 2018, 11:39 AM
Nandhini Manickam
Nandhini Manickam - avatar
+ 1
A= list(map(int,input().Split())).. .what is the meaning for this line
29th Dec 2018, 11:32 AM
Nandhini Manickam
Nandhini Manickam - avatar
0
You're welcome 😀
29th Dec 2018, 11:39 AM
Anna
Anna - avatar