Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python map each integer within input to int

Tags:

python

If I am given an input of 1 2 3 4 5, what is the standard method of splitting such input and maybe add 1 to each integer?

I'm thinking something along the lines of splitting the input list and map each to an integer.

like image 554
user3277633 Avatar asked Sep 22 '15 04:09

user3277633


People also ask

How do you convert input to int in Python?

To convert a string to integer in Python, use the int() function. This function takes two parameters: the initial string and the optional base to represent the data. Use the syntax print(int("STR")) to return the str as an int , or integer.

What is map int input () split ()) in Python?

input() will query the user for input, and read one line of user input; . split() will split that input into a list of "words"; map(int, ...) will call int on each word, it will to that lazily (although that is not important here); and.

How do I get all the elements in an int list?

The most Pythonic way to convert a list of strings to a list of ints is to use the list comprehension [int(x) for x in strings] . It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function.

How do you use the map function for input in Python?

The map() function in Python The map() function (which is a built-in function in Python) is used to apply a function to each item in an iterable (like a Python list or dictionary). It returns a new iterable (a map object) that you can use in other parts of your code.


1 Answers

You may use list comprehension.

s = "1 2 3 4 5"
print [int(i)+1 for i in s.split()]
like image 186
Avinash Raj Avatar answered Nov 09 '22 02:11

Avinash Raj