【发布时间】:2018-08-24 23:25:48
【问题描述】:
我正在尝试编写一个函数来读取具有不同学位的学生志愿者的 CSV 文件。该函数的目的是创建一个字典,其中键是度数,值是度数的频率。
数据组织如下;
name degree email
ABC PhD. abd@gmail.com
CDE Ph.D. cde@gmail.com
FGH MD,PHD fgh@gmail.com
旨在获取字典如下:
#degree_count{'phd':3,'md':1}
def degree_frequency(csv_file):
f = open('csv_file')
csv_f = csv.reader(f)
#Creating a list to store all the degrees from the csv file
student_degree_list=[]
#Creating an empty dictionary to count the frequency
degree_count={}
for row in csv_f:
student_degree_list.append(row[1])
#Replacing fullstops to account for variations in writing degrees ( eg JD vs J.D)
[word.replace(".", "") for word in student_degree_list]
[word.lower() for word in student_degree_list]
for ele in student_degree_list:
if ele in degree_count:
degree_count[ele]=degree_count[ele]+1
else:
degree_count[ele]=0
return degree_count
【问题讨论】:
-
那么您发布的代码有什么问题?
-
@learning_python 你会用熊猫吗?
-
@Aran Frey:我正在交互式平台上尝试。它只是说测试用例失败。没有查明问题。所以我不确定代码有什么问题。
-
@Tanmay Jain:我被明确告知不要使用熊猫。
-
@learning_python 哦哦会编辑答案
标签: python python-3.x dictionary word-frequency