Python中列表元素转为数字的方法分析
作者:zeo 时间:2023-03-20 22:07:46
本文实例讲述了Python中列表元素转为数字的方法。分享给大家供大家参考,具体如下:
有一个数字字符的列表:
numbers = ['1', '5', '10', '8']
想要把每个元素转换为数字:
numbers = [1, 5, 10, 8]
用一个循环来解决:
new_numbers = [];
for n in numbers:
new_numbers.append(int(n));
numbers = new_numbers;
有没有更简单的语句可以做到呢?
1.
numbers = [ int(x) for x in numbers ]
2. Python2.x,可以使用map函数
numbers = map(int, numbers)
如果是3.x,map返回的是map对象,当然也可以转换为List:
numbers = list(map(int, numbers))
3.还有一种比较复杂点:
for i, v in enumerate(numbers): numbers[i] = int(v)
希望本文所述对大家Python程序设计有所帮助。
标签:Python,列表元素,数字
0
投稿
猜你喜欢
MySQL去重的方法整理
2024-01-27 07:04:42
Mac 安装 Python3.10 和 配置环境的详细教程
2021-09-20 08:47:54
Python K-means实现简单图像聚类的示例代码
2023-06-30 10:40:58
asp连接mysql数据库详细实现代码
2012-12-04 19:56:39
python开发之文件操作用法实例
2022-08-07 02:03:45
MYSQL初学者命令行使用指南
2024-01-15 08:46:33
Golang标准库binary详解
2024-04-25 13:19:47
PL/SQL number型数据
2009-02-26 10:59:00
python3.6使用pymysql连接Mysql数据库
2024-01-27 13:00:48
Ubuntu下MySQL安装及配置远程登录教程
2024-01-23 23:55:33
Python使用re模块实现信息筛选的方法
2021-10-10 09:34:46
SpringBoot 中使用JSP的方法示例
2023-06-16 22:35:09
Python实现确认字符串是否包含指定字符串的实例
2022-04-28 07:40:28
C#连接Oracle数据库的实例方法
2024-01-20 14:37:05
Python字符串转换成浮点数函数分享
2022-10-04 01:04:37
js 仿Photoshop鼠标滚轮控制输入框取值(修正兼容Chrome)
2010-02-05 12:27:00
python 字典操作提取key,value的方法
2021-06-01 04:40:39
Python使用正则表达式获取网页中所需要的信息
2023-04-08 17:13:33
JavaScript中诡异的delete操作符
2024-04-10 16:15:33
Python Cookie 读取和保存方法
2021-01-21 15:57:51