首页 > Python资料 博客日记
头歌实践教学平台Python-列表类型翻转教学
2024-05-16 14:00:04Python资料围观244次
本篇文章分享头歌实践教学平台Python-列表类型翻转教学,对你有帮助的话记得收藏一下,看Python资料网收获更多编程知识
第1关 输入数据创建列表
score_ls = []
for i in range(10):
a = float(input())
score_ls.append(a)
print(score_ls)
第2关 计算每位选手的成绩
score_ls = []
for i in range(10):
a = float(input())
score_ls.append(a)
print(score_ls)
print(f'最高分{max(score_ls):.3f},最低分{min(score_ls):.3f},平均分{sum(score_ls) / len(score_ls):.3f}')
第3关 去除一个最高分和一个最低分
score_ls = []
for i in range(10):
a = float(input())
score_ls.append(a)
print(score_ls)
score_ls.remove(max(score_ls))
score_ls.remove(min(score_ls))
print(score_ls)
print(f'最高分{max(score_ls):.3f},最低分{min(score_ls):.3f},平均分{sum(score_ls) / len(score_ls):.3f}')
第4关 计算中位数成绩
score_ls = []
for i in range(10):
a = float(input())
score_ls.append(a)
print(score_ls)
score_ls.remove(max(score_ls))
score_ls.remove(min(score_ls))
score_ls.sort()
print(score_ls)
print(f'去掉最高分和最低分后,中位数成绩为:{(score_ls[len(score_ls)//2]+score_ls[len(score_ls)//2-1])/2:.3f}')
第5关 去除弃权评委成绩
score_ls = []
for i in range(10):
a = input()
score_ls.append(a)
print(score_ls)
while '' in score_ls:
score_ls.remove('')
print(score_ls)
score_ls = list(map(float, score_ls))
score_ls.remove(max(score_ls))
score_ls.remove(min(score_ls))
print(score_ls)
print(f'{max(score_ls):.3f}, {min(score_ls):.3f}, {sum(score_ls) / len(score_ls):.3f}')
第6关 将平均成绩附加到列表末尾
score_ls = []
for i in range(10):
a = input()
score_ls.append(a)
print(score_ls)
while '' in score_ls:
score_ls.remove('')
score = list(map(str, score_ls))
score_ls = list(map(float, score_ls))
score_ls.remove(max(score_ls))
score_ls.remove(min(score_ls))
avg_score = sum(map(float, score_ls))/len(score_ls)
score.append(str(round(avg_score, 2)))
print(score)
第7关 读文件到二维列表
def file_to_lst(file,n):
list1 = []
with open(file) as fr:
for i in range(n):
score_ls = fr.readline().strip()
score_ls = score_ls.split(',')
list1.append(score_ls)
print(list1)
if __name__ == '__main__':
filename = '/data/bigfiles/mark_score.csv'
n = int(input())
file_to_lst(filename,n)
第8关 二维列表数据分析
def file_to_lst(file, n):
"""将文件名变量file指向的文件中的用逗号分隔的数据转为二维列表"""
list1 = []
with open(file) as fr: # 创建文件对象
for i in range(n):
score_ls = fr.readline().strip()
score_ls = score_ls.split(',')
list1.append(score_ls)
return list1
if __name__ == '__main__':
filename = '/data/bigfiles/mark_score.csv'
n = int(input())
list2=[]
for lst in file_to_lst(filename, n): # 遍历二维列表,lst为子列表
while '' in lst: # 循环判定子列表中是否还有空字符串
lst.remove('') # 移除子列表中的空字符串
lst = list(map(float, lst)) # 当前子列表转浮点数
lst.remove(max(lst))
lst.remove(min(lst))
avg_score = sum(map(float, lst))/len(lst)
lst.append(float(round(avg_score, 2)))
list2.append(lst)
print(list2)
第9关 成绩排序
def file_to_lst(file):
with open(file) as fr:
score_ls = [line.strip().split(',') for line in fr]
return score_ls
def del_null_string(score_ls):
score_ls_float = []
for lst in score_ls:
while '' in lst:
lst.remove('')
lst = list(map(float,lst))
score_ls_float.append(lst)
return score_ls_float
def add_avg(score_ls_float):
for i in range(len(score_ls_float)):
score_ls_float[i].remove(max(score_ls_float[i]))
score_ls_float[i].remove(min(score_ls_float[i]))
avg = sum(score_ls_float[i]) / len(score_ls_float[i])
score_ls_float[i].append(round(avg,2))
return score_ls_float
if __name__ == '__main__':
filename = '/data/bigfiles/mark_score.csv'
n = int(input())
score_lst = file_to_lst(filename)
score_lst_f = del_null_string(score_lst)
score_avg = add_avg(score_lst_f)
print(sorted(score_avg, key=lambda x: (x[-1],-x[-2]),reverse=True)[:n])
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- python-kaggle商场顾客细分数据分析
- python 实现信号高通、低通、带通滤波处理代码,并画出滤波后的时域频域图
- 【实战】Python+OpenCV车道线检测识别项目:实现L2级别自动驾驶必备(配套课程+平台实践)
- 深入了解 AWT 中的 Menu(菜单)组件
- 大数据-215 数据挖掘 机器学习理论 - KMeans Python 实现 算法验证 sklearn cluster.cluster_centers_ inertia_
- python视频格式转换时,报错:ModuleNotFoundError: No module named ‘moviepy.editor‘
- python谷歌浏览器dino游戏,完整开源代码
- Python 给 Excel 写入数据的四种方法
- 【Python】成功解决AttributeError: ‘list‘ object has no attribute ‘split‘
- 豆包 API 调用示例代码详解-Python版
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Anaconda版本和Python版本对应关系(持续更新...)
- Python与PyTorch的版本对应
- Windows上安装 Python 环境并配置环境变量 (超详细教程)
- Python pyinstaller打包exe最完整教程