0
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(':')

4 Answers 4

7
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.

1
  • 2
    beat me by 3 seconds ;) Commented Mar 6, 2014 at 9:38
3

Use a list comprehension:

[ int(token) for token in y.split(':') ]
3

You can use list comprehensions:

y,m,d = [int(n) for n in x.split(':')]
2

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.