Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
x='2013:02:01' y,m,d=x.split(':')
Produces y,m,d as strings. But how do I produce them as ints using only 1 line
Failed:
y,m,d=int(y.split(':')) y,m,d=int(y),int(m),int(d)=y.split(':')
y, m, d = map(int, x.split(':'))
map applies the function to each of the elements in the iterable. In this case it will apply int to each of the values returned by split and give you the result.
map
int
split
Add a comment
Use a list comprehension:
[ int(token) for token in y.split(':') ]
You can use list comprehensions:
y,m,d = [int(n) for n in x.split(':')]
Using list comprehension will do the trick:
>>> x='2013:02:01' >>> y,m,d=[int(n) for n in x.split(':')] >>> y 2013 >>> d 1 >>> m 2
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.