python 数组转列表
时间: 2023-10-14 20:10:26 浏览: 49
在Python中,数组可以通过以下方式转换为列表:
```python
# 导入numpy库
import numpy as np
# 创建一个数组
arr = np.array([1, 2, 3, 4, 5])
# 将数组转换为列表
list = arr.tolist()
print(list)
```
输出结果:
```
[1, 2, 3, 4, 5]
```
另外,如果你使用Python标准库中的数组,也可以通过以下方式将其转换为列表:
```python
# 导入array模块
from array import array
# 创建一个数组
arr = array('i', [1, 2, 3, 4, 5])
# 将数组转换为列表
list = arr.tolist()
print(list)
```
输出结果:
```
[1, 2, 3, 4, 5]
```
相关问题
python 数组转列表tolist
要将Python中的数组转换为列表,可以使用numpy库中的tolist()函数。具体的代码如下:
```
import numpy as np
# 创建一个数组
a = np.array([1, 2, 3, 4, 5])
# 数组转化为列表
b = a.tolist()
print(b)
```
运行以上代码,即可将数组a转换为列表b,并输出结果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [python:数组和列表相互转化](https://blog.csdn.net/weixin_39450145/article/details/111496017)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *3* [Python数组转字符串互转详细操作](https://blog.csdn.net/mycocoo/article/details/115213445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
python数组和列表的转换
在Python中,可以使用list()函数将数组转换为列表,也可以使用numpy库中的tolist()函数将数组转换为列表。
例如,将数组a转换为列表:
```python
import numpy as np
a = np.array([1, 2, 3])
lst = a.tolist()
print(lst)
```
输出结果为:
```python
[1, 2, 3]
```
同样地,使用list()函数也可以将数组转换为列表:
```python
a = [1, 2, 3]
lst = list(a)
print(lst)
```
输出结果为:
```python
[1, 2, 3]
```
如果要将列表转换为数组,可以使用numpy库中的array()函数。例如,将列表lst转换为数组:
```python
lst = [1, 2, 3]
a = np.array(lst)
print(a)
```
输出结果为:
```python
[1 2 3]
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)