题目描述:
任务描述
题目文件中保存来自约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(',', maxsplit=1))
return ls
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条 |
答案如下:
cmd=input()
if cmd =='记录':
print(f'总计:{len(lt)}条')
elif cmd== '程序员':
a=len([i for i in lt if 'I am a developer by profession' in i[1]])
print(f'程序员:{a}条')
elif cmd=='程序爱好者':
b=len([i for i in lt if 'I code primarily as a hobby' in i[1]])
print(f'程序爱好者:{b}条')
elif cmd=='程序初学者':
c=len([i for i in lt if 'I am a student who is learning to code' in i[1]])
print(f'程序初学者:{c}条')
elif cmd=="编程相关者":
d=len([i for i in lt if "I am not primarily a developer, but I write code sometimes as part of my work" in i[1]])
print(f'编程相关者:{d}条')
elif cmd=='非程序员':
e=len([i for i in lt if 'I used to be a developer by profession, but no longer am' in i[1]])
print(f'非程序员:{e}条')
elif cmd=='空白':
f=len([i for i in lt if 'NA' in i[1]])
print(f'空白:{f}条')
else:
print('错误输入')