赞
踩
题目文件中保存来自约60000条程序员的问卷调查记录(部分数据),数据来源:2020 Stack Overflow Developer Survey
调查问题为:
文件中数据格式为: 每行数据分为两部分,调查编号和工作描述 题目提供一个fopen函数打开该文件,将数据存入列表并返回。
#不允许修改。fopen函数打开文件,并返回包含文件数据一个列表ls,
def fopen(name):
ls=[]
with open(name,'r',encoding = 'UTF-8') as f:
for i in f.readlines()[1:]:
ls.append(i.strip().split(','))
return ls
def fun(t): #自定义函数完成相关功能,函数名或形参或函数数量可自行修改
... ...
... ...
def ...
lt = fopen('survey.csv') #不允许修改,打开题目文件,并将返回的数据列表赋值给lt
返回列表形式为: [['1', 'I am a developer by profession'], ['2', 'I am a developer by profession'], ['3', 'I code primarily as a hobby'], ['4', 'I am a developer by profession'], ['5', '"I used to be a developer by profession', ' but no longer am"'], ... ...] 请输入下列分类,统计问卷数据中下列各项分类的记录条数:
分类 回答 程序员 ‘I am a developer by profession’ 程序爱好者 ‘I code primarily as a hobby’ 程序初学者 ‘I am a student who is learning to code’ 编程相关者 ‘"I am not primarily a developer, but I write code sometimes as part of my work"’ 非程序员 ‘"I used to be a developer by profession, but no longer am"’ 空白 ‘NA’ 如果输入为'记录',输出所有记录条数
如果不是以上的输入,则输出‘错误输入’
输入输出示例仅为格式示例,与实际测试数据无关
输入 | 输出 | |
---|---|---|
示例 1 | 成员 | 错误输入 |
示例 2 | 程序爱好者 | 程序爱好者:23457条 |
示例 3 | 记录 | 总计:66887条 |
示例 4 | 空白 | 空白:4567条 |
-
- #不允许修改。fopen函数打开文件,并返回包含文件数据一个列表ls,
- def fopen(name):
- ls=[]
- with open(name,'r',encoding = 'UTF-8') as f:
- for i in f.readlines()[1:]:
- ls.append(i.strip().split(','))
- return ls
- lt = fopen('survey.csv') #不允许修改,打开题目文件,并将返回的数据列表赋值给lt
-
- def fun(ls):
- person_no_hair=0
- like_no_hair=0
- new_no_hair=0
- rele_no_hair=0
- no_no_hair=0
- kong=0
- for i in ls:
- if i[1]=='I am a developer by profession':#程序员
- person_no_hair+=1
- elif i[1]=='I code primarily as a hobby':#程序爱好者
- like_no_hair+=1
- elif i[1]=='I am a student who is learning to code':#程序初学者
- new_no_hair+=1
- elif i[1]=='"I am not primarily a developer':#编程相关者
- rele_no_hair+=1
- elif i[1]=='"I used to be a developer by profession':#非程序员
- no_no_hair+=1
- elif i[1]=='NA':
- kong+=1
- return [person_no_hair, like_no_hair, new_no_hair, rele_no_hair, no_no_hair,kong]
-
- s=input()
- l=fun(lt)
- if(s=='程序员'):
- print(f"程序员:{l[0]}条")
- elif(s=='程序爱好者'):
- print(f"程序爱好者:{l[1]}条")
- elif(s=='程序初学者'):
- print(f"程序初学者:{l[2]}条")
- elif(s=='编程相关者'):
- print(f"编程相关者:{l[3]}条")
- elif(s=='非程序员'):
- print(f"非程序员:{l[4]}条")
- elif(s=='空白'):
- print(f"空白:{l[5]}条")
- elif(s=='记录'):
- print(f"总计:{sum(l)}条")
- else:
- print("错误输入")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。