所以我正在开发一个打开外部文件的程序,然后运行它以查看它是否包含特定信息。有没有办法简化它,或者它现在是最有效的方式来写这个?
def printGender(alist):
if "Female" in alist:
print(alist)
print("Female Students")
def maleInfo(blist):
if "2010" in blist:
print(blist)
print("Students who enrolled in 2010")
def csc2010(clist):
if "CSC" in clist and "2010" in clist and "Female" in clist:
print(clist)
print("Female students who registered in CSC in 2010")
def main():
ref = open("file1.txt","r")
studentList = ref.readlines()
ask = 10
while ask != 0:
print("1) print all female info")
print("2) display all male info from 2010")
print("3) display female students who registered for CSC in 2010")
ask = int(input("Enter option 1, 2, 3 or 0 to quit: "))
if ask == 1:
for i in range(len(studentList)):
alist = studentList[i]
printGender(alist)
elif ask == 2:
for i in range(len(studentList)):
blist = studentList[i]
maleInfo(blist)
elif ask == 3:
for i in range(len(studentList)):
clist = studentList[i]
csc2010(clist)
elif ask == 0:
print("You chose to quit")
break
else:
print("Not a Valid input")
continue
ref.close()
main()
有没有办法简化这段代码,这样我就不会在main函数中创建三个单独的列表。
if ask == 1:
for i in range(len(studentList)):
alist = studentList[i]
printGender(alist)
elif ask == 2:
for i in range(len(studentList)):
blist = studentList[i]
maleInfo(blist)
elif ask == 3:
for i in range(len(studentList)):
clist = studentList[i]
csc2010(clist)
elif ask == 0:
print("You chose to quit")
break
else:
ect...
我很想知道是否有更短的方法来获得相同的结果。也许使用运行该部分代码的函数,但我不知道该怎么做。
答案 0 :(得分:1)
需要注意的一些问题:
构造
for i in range(len(studentList)):
alist = studentList[i]
printGender(alist)
非常讨厌;如果你确实需要i
,你应该使用
for i, student in enumerate(student_list):
print_gender(student)
,否则
for student in student_list:
print_gender(student)
你的功能名不佳;他们不按照他们的说法去做! printGender
打印女学生,printMale
打印2010年的学生等。同样,您的变量名称选择不当; alist
不是学生列表,只有一名学生。
您似乎每个学生都有一个文字字符串,类似于2009, 176915, Jones, Susan, Female, CSC
;但你没有尝试将字段分开。这将导致像2009, 292010, Male, Jill, Female, RCSCA
这样的学生在2009年和2010年(学生编号的错误匹配)以及女性和男性(姓氏的错误匹配)以及CSC中被报告为学生的恼人问题(课程名称的错误匹配)。你真的需要使用更好的数据格式 - 无论是.csv还是.json还是数据库,还有任何返回命名字段的东西 - 来解决这个问题。
您的搜索选项是非正交的,仅限于预编码选项;例如,你没有办法搜索2007年的所有CSC学生而没有重写你的课程。
修复这些问题会导致类似
的问题import json
def record_print_format(record):
return "{Year:4} {Id:6} {Gender:6} {Firstname:>20} {Lastname:<20} {Course:6}".format(**record)
def show_records(records, format_fn=record_print_format):
for r in records:
print(format_fn(r))
num = len(records)
print("{} records:".format(num))
def filter_records(records, field, value):
return [r for r in records if r[field] == value]
def match_year(records, year):
return filter_records(records, "Year", year)
def match_gender(records, gender):
return filter_records(records, "Gender", gender)
def match_course(records, course):
return filter_records(records, "Course", course)
def main():
with open("student_list.json") as studentfile:
all_students = json.load(studentfile)
records = all_students
while True:
print("1: Filter by year")
print("2: Filter by gender")
print("3: Filter by course")
print("8: Show results")
print("9: Clear filters")
print("0: Exit")
option = input("Please pick an option: ").strip()
if option == "1":
year = input("Which year? ").strip()
records = match_year(records, year)
elif option == "2":
gender = input("Which gender? [Male|Female] ").strip()
records = match_gender(records, gender)
elif option == "3":
course = input("Which course? ").strip()
records = match_course(records, course)
elif option == "8":
show_records(records)
elif option == "9":
records = all_students
elif option == "0":
print("Goodbye!")
break
else:
print("I don't recognize that option.")
if __name__=="__main__":
main()