Python | 列表相关

1. 将列表中逆序输出

代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

list = ['a', 'b', 'c', 'd']
list.reverse()
print(list)

输出:

['d', 'c', 'b', 'a']

 

2. 将数组中的数字按小到大的顺序排列

代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

l = [231,345,12,536,23,356,73,131,135,676,8352,13]
l.sort()
print(l)

输出:

[12, 13, 23, 73, 131, 135, 231, 345, 356, 536, 676, 8352]

 

3. 按逗号分割列表

代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

L = [1, 2, 3, 4, 5]
L = repr(L)[1:-1]
print L

输出:

1, 2, 3, 4, 5

 

posted @ 2020-06-16 15:45  陈昌鸿  阅读(128)  评论(0编辑  收藏  举报