简体   繁体   English

如何将numpy数组元素从字符串转换为int

[英]How to convert numpy array elements from string to int

I have a list of numbers in string format.我有一个字符串格式的数字列表。 I converted that list into numpy array using np.asarray() .我使用np.asarray()将该列表转换为 numpy 数组。

How do I convert the string elements to ints?如何将字符串元素转换为整数?

如果您有x = np.matrix ,其中每个元素都是 '1.0' (作为str )并且您想将其转换为intfloat

x = x.astype(np.float)
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. nums_int_np - 现在是 np 整数数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM