将1到20的数字字符串按照实际大小排序 python
时间: 2023-05-28 12:05:13 浏览: 102
可以使用sort()函数对数字列表进行排序,代码如下:
```
nums = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
nums.sort()
print(nums)
```
输出结果为:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
```
如果将数字字符串按照实际大小排序,可以先将字符串转换为数字,然后再排序,代码如下:
```
nums_str = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']
nums = [int(num) for num in nums_str]
nums.sort()
print(nums)
```
输出结果为:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
```
阅读全文