python-3.x - How to get rid of brackets around values within a numpy array? - Python
问题描述
I'm working with some climate data at the moment, but it comes in a weird shape. The arrays look the following:
([[0.02115021]],
[[0.03046454]],
[[0.05636626]],
[[0.08100581]],
[[0.1113209 ]],
[[0.11042633]],
[[0.12332429]],
[[0.1256145 ]],
[[0.13792552]],
[[0.11826107]],
[[0.05710823]]],
mask=False,
fill_value=1e+20,
dtype=float32)
But I want just a simple numpy array looking like ([1,2,3,4,5,6,7]), since this is a time series. I tried to convert it with np.asarray(data), but the double brackets around the values are still there, what makes working with the data kinda impossible. Does anybody has an idea how to get rid of them?
Thanks a lot.
解决方案
numpy 数组的 Flatten 方法可用于将 nd 数组转换为 1d 数组。
a = np.array([[1,2],[3,4]])
a.flatten()
# output: array([1, 2, 3, 4])
更多信息,请访问 https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html
推荐阅读
- python - How to create a new list from values from a existing list
- html - Is there a way to use Bootstrap ordering to change what column an element is in in a different breakpoint?
- python - Is it a good practice to have an intensive __init__ method in a python class?
- laravel - Accessing private file from local storage (laravel) by Axios call in Vue.js
- arrays - 用 ionic 3 中的两个值过滤数组
- python - “while”循环的问题
- android - 如果 OpenAPI 生成器的 Moshi 中的值不匹配,如何回退枚举
- javascript - 如何在Javascript中恢复插入符号位置?
- python - 如何使用python解析.in文件并将内容写入.out文件
- scala - 将 scala 映射值与列表进行比较,并为列表中不存在的键返回默认值