python里让列表内的字符串类型数字转换为整数类型数字
时间: 2023-03-20 11:00:29 浏览: 197
可以使用Python内置的map函数,将列表中的每个字符串类型数字转换为整数类型数字。具体代码如下:
```
lst = ['1', '2', '3', '4']
lst = list(map(int, lst))
print(lst)
```
运行结果为:
```
[1, 2, 3, 4]
```
其中,`map(int, lst)` 表示将 `lst` 中的每个元素都应用 `int` 函数进行转换。由于 `map` 函数返回的是一个可迭代对象,因此需要使用 `list` 函数将其转换为列表。最后输出结果即可。
相关问题
python里面怎么让列表里的字符型数字变为整数型数字
在Python中,可以使用int()函数将列表中的字符串类型数字转换为整数类型数字。例如,如果有一个包含字符串数字的列表,如下所示:
```
num_list = ['1', '2', '3', '4']
```
则可以使用以下代码将其转换为整数类型数字列表:
```
num_list = [int(num) for num in num_list]
```
这将输出以下结果:
```
[1, 2, 3, 4]
```
在上面的代码中,我们使用了列表推导式和int()函数来将字符串类型数字转换为整数类型数字。
阅读全文
相关推荐
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)