把array变为list
时间: 2023-08-14 21:04:36 浏览: 38
要将一个array转换为list,可以使用Python中的内置函数list()。例如,如果有一个NumPy数组a,可以将其转换为Python列表的方法如下:
```
import numpy as np
a = np.array([1, 2, 3, 4, 5])
lst = list(a)
print(lst) # [1, 2, 3, 4, 5]
```
这样,就可以将NumPy数组a转换为Python列表lst。
相关问题
python如何把array变成list
可以使用Python内置函数list()将array转换为list,例如:
```python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
lst = list(arr)
print(lst) # [1, 2, 3, 4, 5]
```
python怎么把list变为array
可以使用numpy库中的array()函数将list转换为array。具体操作如下:
1. 导入numpy库
```python
import numpy as np
```
2. 定义一个list
```python
my_list = [1, 2, 3, 4, 5]
```
3. 使用array()函数将list转换为array
```python
my_array = np.array(my_list)
```
转换后,my_array就变成了一个numpy数组。