python-组织列表

1.使用sort()对列表进行永久排序

通过首字母对列表中的元素进行排列

list.sort(reverse=True) 相反排序

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

2.使用sorted()对列表进行临时排序

 

list = ['a','c','e','d','b']
print (sorted(list))
print (sorted(list,reverse=True))
['a', 'b', 'c', 'd', 'e']
['e', 'd', 'c', 'b', 'a']

 

调用函数sorted后,源列表元素顺序不变

3.倒着打印列表

  反转列表中的元素的排列顺序,使用方法reverse()

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

4.确定列表长度

list = ['a','c','e','d','b']
print
(len(list))
5

 

posted @ 2020-08-13 12:54  阿鑫的哥哥  阅读(77)  评论(0)    收藏  举报