sorted()函数:
# sorted()默认为升序排列,reverse=True指定为降序排列
# sorted()函数可以对list,dict,array,tuple进行排序
# 例子:
# (1)排序字符串列表
# scores = ['A', 'B', 'C', 'D']
# scores = sorted(scores, reverse=True)
# 得:['D', 'C', 'B', 'A']
# (2)排序字典
# 按照关键字来排序
# scores = {'A': 3, 'B': 2, 'C': 1}
# scores = sorted(scores, reverse=True)
# 得:['C', 'B', 'A']
# (3)排序数组
# 一维数组排序跟列表一样
# 多维数组排序按照每一行的首元素来排序
# (4)嵌套列表
# 不管是列表中嵌套元组还是列表中嵌套列表,都按照列表中每个元素的首元素来排序
# 即sorted可以实现元组间和列表间排序,但不能实现在字典间排序
# scores = [(0, 5), (1, 4), (2, 3)]
# scores = sorted(scores, reverse=True)
# 得:[(2,3), (1, 4), (0, 5)]
# scores = [[1, 9, 7], [3, 5, 8], [2, 10, 5]]
# scores = sorted(scores, reverse=True)
# 得:[[3, 5, 8], [2, 10, 5], [1, 9, 7]]
round()函数:
# round(number, ndigits)表示将number转换为指定精度的数字,ndigits表示精度的位数。
# 如果number本身的精度小于指定的精度则返回它本身。
# 比如:round(5.2, 2)返回5.2
# 如果number本身的精度大于指定的精度则将number变为指定的精度返回,并且进行四舍五入。
# 比如:round(5.2056, 2)返回5.21,进行了四舍五入。