python - 如何在 numpy 中仅将字符串数组的整数转换为 float 数组?

标签 python arrays list numpy

如何转换

["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/

相关文章:

java - Arraylist 到 2D 数组,然后到 JTable

python - 如何在 python pandas 中使用列表中的元素复制行

c - 使用二维数组写访问冲突

python - Python中未知长度列表的变量赋值

delphi - 在 Delphi 中将静态数组作为动态数组的参数传递

javascript - 不同数组上的map()结果不同

python - 附加到 Python 列出每个列表项的所有可能转换

python - 需要 'package' 参数来执行相对导入

python - 在 YOLO 中无法获得正确的 yolo 预训练权重

python - 类型错误 : __init__() missing 1 required positional argument: 'on_delete' (Django 2)