python - Slicing Numpy ndarray
问题描述
I have used numpy to split a string at each ,
. The output shows the split words. When I want to retrive first element, I get the following error:
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed.
Code
item = "3C2B,FF8BFF5F008C,64,2021-08-01T18:00:01Z,2a* "
item_value = np.char.split(item, sep = ',')
print(item_value)
> ['3C2B', 'FF8BFF5F008C', '64', '2021-08-01T18:00:01Z', '2a*']
print(type(item_value))
> <class 'numpy.ndarray'>
device_id = item_value[0]
Output:
IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed.
Expected output: 3C2B
解决方案
使用np.str.split
而不是np.char.split
.
以下代码工作正常。
item = '3C2B,FF8BFF5F008C,64,2021-08-01T18:00:01Z,2a*'
item_value = np.str.split(item, sep = ',')
device_id = item_value[0]
print(device_id)
输出:
3C2B
推荐阅读
- ios - 如何检测用户何时终止应用程序 - Swift
- python - pydrive 不接受来自我的 Google Cloud 帐户的 client_secret.json
- javascript - 使用 Hapi、axios 和 Cheerio 时 JSON 输出中的编码错误
- c# - ASP.NET Core API:添加自定义路由令牌解析器
- http - 有没有办法让 wget 完全模仿浏览器?
- node.js - Node js在后台使用ram
- reactjs - 带有react-draft-wysiwyg的多级formik表单不显示内容
- c# - [全文搜索]实现全文搜索
- r - 将 %in% 的结果制成表格
- apache-kafka - 如何记录 Kafka 主题访问