Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Python equivalent of map in Ruby and Javascript?

Tags:

python

list

map

Say for example I want to split string "12:30-14:40" and have the result in a matrix like: [["12","30"],["14","40"]].

I can do this in JavaScript with:

"12:30-14:40".split("-").map(function(x) {
    return x.split(':');
});

and in Ruby with:

 "12:30-14:40".split("-").map{|x| x.split(":")}

What would be the python equivalent for the above?

like image 811
Eduard Florinescu Avatar asked Nov 07 '12 14:11

Eduard Florinescu


1 Answers

You can use a list comprehension:

>>> [i.split(':') for i in "12:30-14:40".split('-')]
[['12', '30'], ['14', '40']]
like image 65
Nicolas Avatar answered Sep 24 '22 14:09

Nicolas