5

I have a list of numbers in string format. I converted that list into numpy array using np.asarray().

How do I convert the string elements to ints?

4
  • 4
    np.asarray(listx, dtype = int)? Given that the strings are integers. Commented Jan 22, 2018 at 21:07
  • It seems to be working, Thanks a lot. Commented Jan 22, 2018 at 21:13
  • Possible duplicate of Convert all strings in a list to int Commented Jan 22, 2018 at 21:14
  • @zipa I have tried 'map' to convert but it's completing my purpose. Commented Jan 22, 2018 at 21:22

2 Answers 2

3

If you have x = np.matrix, where each element is '1.0' (as str) and you want to convert it to int or float:

x = x.astype(np.float)
Sign up to request clarification or add additional context in comments.

Comments

2
import numpy as np
nums_str = ['1','23','345']
nums_str_np = np.asarray(nums_str)
nums_int_np = nums_str_np.astype('int')

nums_int_np - is now np array of integers.

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.