Do you want to separate several numbers? 1 2 3 -> [1, 2, 3]
n = str(input())
n = n.split()
numbers = [int(i) for i in n]
print(numbers)
Or split a number in numeral? 123 -> [1, 2, 3]
n = str(input())
numbers = [int(i) for i in n]
print(numbers)
Use Nikhil answer, if you want to split a number with delimiters 1%3 -> [1, 3]
Answer from Sergej Schelle on Stack OverflowDo you want to separate several numbers? 1 2 3 -> [1, 2, 3]
n = str(input())
n = n.split()
numbers = [int(i) for i in n]
print(numbers)
Or split a number in numeral? 123 -> [1, 2, 3]
n = str(input())
numbers = [int(i) for i in n]
print(numbers)
Use Nikhil answer, if you want to split a number with delimiters 1%3 -> [1, 3]
You can split integer value with following ways..
list comprehension
n = str(input()) result = [x for x in n] print(result)
using list object
n = str(input()) result = [x for x in n] print(result)using map object
n = str(input()) result = list(map(int,n)) print(result)
Convert the number to a string so you can iterate over it, then convert each digit (character) back to an int inside a list-comprehension:
>>> [int(i) for i in str(12345)]
[1, 2, 3, 4, 5]
return list of strings
>>> list(str(12345))
['1', '2', '3', '4', '5']
return list integers
>>> map(int, str(12345))
[1, 2, 3, 4, 5]
How to split int into digits?
can i split a string of user input, into individual intergers?
python - How to split an integer? - Stack Overflow
python 3.x - How Can I Separate Two Input Integers? - Stack Overflow
I want to convert 19 into [1, 9]. I tried split but on a '19' but gives me same '19'.
Actually, there's an even simpler solution than any of these: convert number to list
import numpy as np
num1 = 1234
num2 = 1245
a = list(str(num1))
b = list(str(num2))
// a = [1,2,3,4]
// b = [1,2,4,5]
c = np.in1d(a,b)
The resulting c is then:
array([ True, True, False, False], dtype=bool)
I think you would be best off converting the numbers to strings and then comparing each character:
userInput = 6908
answer = 6709
userInputStr = str(userInput)
answerStr = str(answer)
outputStr = " "
print(outputStr.join(["Y" if i == j else "N" for i, j in zip(userInputStr, answerStr)]))
You can use modulus operator % and division operator / and form the divisor as 10^n where n is number of digits in each result of split.
Convert them to a string and then split them. Like:
Use with AIs = str(212313454)
a = s[0:3]
b = s[3:6]
c = s[6:]
Use a loop for a variable length number.
You can use the split() method of the string object, which defaults to splitting on whitespace characters. This will give you a list of separate strings. To convert each string to an integer, you could use a comprehension. Assuming the input is in correct form, the following one-liner will do it:
x, y, z = ( int(coord) for coord in xyz1.split() )
You can take the string that is in xyz1 and split it and turn them into integers like this
xyz_list = [int(x) for x in xyz1.split(' ')]
If you don't want these integers in a list and would prefer to store them into separate variables, just do this
x = xyz_list[0]
y = xyz_list[1]
z = xyz_list[2]
You can do it with string methods fairly easily, as other answers have already shown. I direct you to the related grouper recipe in itertools.
I want to mention that it may be more efficient to do it with maths:
>>> n = 45346
>>> output = []
>>> while n:
... output.append(n % 100)
... n //= 100
...
>>> output = output[::-1]
>>> print output
[4, 53, 46]
s = raw_input()
if(len(s)%2 ==1):
s = '0'+s
list = []
for i in range(len(s)/2):
list.append(int(s[2*i:2*i+2]))
print list
After taking the input string, check if its of odd length, and if yes, prefix a 0 to it. Then , iterate over the string , taking two characters at a time , cast that substring to int , and append it in a list. It's that simple.