I'm working in Python, but coming from an R background - where, if I want to take an array of strings x = ['1', '2', '3']
and get the corresponding array of integers [1, 2, 3]
, the natural thing to do would be to run something like int(x)
. Of course, this syntax doesn't work because I'm in Python and functions aren't automatically vectorized. Seems like I can maybe use NumPy to create a vectorized version of functions, but that feels like speaking Python in a very heavy R accent.
Looping over the contents of the vector works
x2 = []
for y in x:
x2.append(int(y))
but doesn't seem at all elegant - it's a three line construction for something I could do in six characters in R. Surely there's a Pythonic way to do this that's more compact? Or is this butting up against the respective strengths and weaknesses of Python vs. R?
You can either use map
>>> lst = ['1', '2', '3']
>>> map(int, lst)
[1, 2, 3]
or a list comprehension:
>>> [int(x) for x in lst]
[1, 2, 3]
Which one to use? Primarily opinion based. Personally, I prefer map
if the function that is mapped already exists as a built-in.
Note that in Python3, map
will yield a map object, which - if you want to have a list - you would have to cast to list explicitly. So the comprehension is probably preferred in Python3:
>>> result = map(int, ['1', '2', '3'])
>>> result
<map object at 0x7f35c4c3af98>
>>> list(result)
[1, 2, 3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With