python小题目练习(八)
题目:电视剧的收视率排行榜
需求:实现如下图所示需求
代码展示:
"""
Author:mll
Content:电视剧的收视率排行榜
Date:2020-11-16
"""
# 定义列表存储数据,每条数据存储在元祖中
TV_list = [('《Give up, hold on to me》收视率:', '1.4%'), ('《The private dishes of the husbands》收视率:', '1.343%'),
('《My father-in-law will do martiaiarts》收视率:', '0.92%'),
('《North Canton still believe in love》收视率:', '0.862%'),
('《Impossible task》收视率:', '0.553%'), ('《Sparrow》收视率:', '0.411%'), ('《East of dream Avenue》收视率:', '0.164%'),
('《The prodigal son of the new frontier town》收视率:', '0.259%'), ('《Distant distance》收视率:', '0.394%'),
('《Music legend》收视率:', '0.562%')]
# q = sorted(TV_list, key=lambda x: x[1], reverse=True) # 内建函数sorted()不改变原列表,生成新列表
TV_list.sort(key=lambda x: x[1], reverse=True) # 内建方法sort()更改原列表,原列表元素位置发生改变
print('电视剧的收视率排行榜:')
for i in TV_list:
for j in range(len(i)):
if j == 1:
print(i[j])
else:
print(i[j], end='')
截图展示:
总结:两种排序方法都可以,但是有区别,视情况选用