Python将列表中的元素转化为数字并排序的示例
2020-03-18 12:09
1351 查看
本文实例讲述了Python中列表元素转为数字的方法。分享给大家供大家参考,具体如下:
有一个数字字符的列表:
numbers = ['2', '4', '1', '3']
想要把每个元素转换为数字:
numbers = [2, 4, 1, 3]
1. Python2.x,可以使用map函数:
numbers = map(int, numbers)
2. Python3.x,map返回的是map对象,当然也可以转换为List:
numbers = list(map(int, numbers))
排序:
使用sorted函数,从小到大排序:
numbers = sorted(numbers, reverse = False)
从大到小排序:
numbers = sorted(numbers, reverse = True)
以上这篇Python将列表中的元素转化为数字并排序的示例就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- Python常见排序操作示例【字典、列表、指定元素等】
- Python中对元组和列表按条件进行排序的方法示例
- Python中列表元素转为数字的方法分析
- Python list列表中删除多个重复元素操作示例
- Python中对列表中的字典元素进行排序
- Python中关于列表里的字典元素排序问题
- Python中列表元素转为数字的方法分析
- python基础编程_26_列表元素的插入,删除,排序
- Python对列表中字典元素排序
- Python中对元组和列表按条件进行排序的方法示例
- python入门:列表元素绝对值排序
- Python中对元组和列表按条件进行排序的方法示例
- python学习每日一题【20200302】python实现元素为字典的列表的排序
- leetcode_452. Minimum Number of Arrows to Burst Balloons 用最小的箭击破气球,python列表按元素排序
- Python实现的列表排序、反转操作示例
- Python实现删除列表中满足一定条件的元素示例(转)
- python 统计列表(list)中元素出现的次数,并排序
- python 实战12 列表元素绝对值排序
- Python实现删除列表中满足一定条件的元素示例
- Python:数字字符串和整型列表之间的转化