如何转换
["5", "3","Code", "9","Pin"]
至
[5, 3,"Code", 9,"Pin"]
在 NumPy 中?
类似于这个问题How to convert an array of strings to an array of floats in numpy?除了有文字之外还有什么办法吗?
最佳答案
您可以使用列表理解来检查列表中的元素是否为数字,并相应地返回字符串或 int
:
l = ["5", "3","Code", "9","Pin"]
[int(i) if i.isnumeric() else i for i in l]
输出
[5, 3, 'Code', 9, 'Pin']
关于python - 如何在 numpy 中仅将字符串数组的整数转换为 float 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54791203/