1:使用sort对列表永久排序
例:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#coding=utf-8
#要插入中文注释,必须在前面加上这三行
cars=['bmw','audi','toyota']
#假设所有列表里的都是小写字母
cars.sort()
print(cars)
#永久对cars排序,且不可复原
cars.sort(reverse=True)
print(cars)
sorted函数临时排序,可恢复
cars.sort(reverse=True)
print(cars)
cars=['bmw','audi','toyota']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
若是倒过来排序,则用
`print(sorted(cars,reverse=True))
#倒着排序
cars.reverse()
print(cars)
#reverse 为永久性排列
#reverse 为永久性排列
#获取列表的长度,len函数
count=len(cars)
print("the number",count)