这是python 3代码, 有2个输入文件courses.txt和student.txt,我能够从一个列表中的courses.txt和另一个列表中的学生信息中读取课程信息。 现在,我想创建第三个列表,在该列表中应将特定学期的学生注册到该课程。
#course registration
list_courses=[]
with open("courses.txt",'r') as myfile1:
for line in myfile1:
list_courses.append(line.strip().split(','))
list_student=[]
with open("students.txt",'r') as myfile1:
for line in myfile1:
list_student.append(line.strip().split(','))
list_final=[]
for index,student in enumerate(list_student):
semester=student[0]
roll_number=(student[1])
print (("semester=%s roll_number=%s")%(semester,roll_number))
if roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs'):
list_final=list_student[index]+list_courses[index2]
print (list_final)
courses.txt文件是
cs101,C programming
cs102,Digital logic and design
cs103,Electrical engineering
cs231,IT networks
cs232,IT Workshop
cs233,IT programming
cs301,Compilers and automata
cs302,Operating Systems
cs303,Networks
cs401,Game Theory
cs402,Systems Programming
cs403,Automata
ec101,Digitization
ec102,Analog cicuit design
ec103,IP Telephony
ec201,Wireless Network
ec202,Microwave engineering
ec203,Antenna
ec301,Maths2
ec302,Theory of Circuits
ec303,PCB design
ec401,PLC programming
ec402,Scada
ec403,VLSI
带有数字的课程表示该课程的学期,即cs101表示计算机科学专业的学生,101中的前1表示学期1, 如果cs202意味着cs的第二学期课程。
students.txt文件如下
4,cs2313,Someone
7,ec3325,Someone
7,cs4890,Someone
1,ec1008,Someone
3,cs2002,Someone
3,cs2009,Someone
数字4,7代表学期,行中的第二个元素代表卷号,第三个元素代表学生的姓名,现在我想要一个新列表,其中基于学期,我已将课程添加到学生信息中,即以下记录输入list3 7,ec3325,Someone,ec301,ec302,ec303
我无法理解以下部分中的更改
for index,course in enumerate(list_courses):
print(index,course[0])
if(course[0].statswith(cs1))
以便list3 = list.append(list2 append(以cs1或所需的开头的课程)值)
输出为
['3', 'cs2009', 'someone', 'cs403', 'Automata']
输出应该是
['3', 'cs2009', 'someone', 'cs301','Compilers and automata'
'cs302','Operating Systems','cs303,'Networks']
请注意,由于列表中的第一个条目是3(学期),因此课程从cs3xx开始
我在哪里犯错?
答案 0 :(得分:0)
这是我最终获得期望结果的方式。但是,可以减少答案中多个if语句的任何建议都是受欢迎的,也是可以接受的。因此,如果有更好的方法,请回答。
#course registration
list_courses=[]
with open("courses.txt",'r') as myfile1:
# list_courses=myfile1.readlines()
# list_courses=[x.strip() for x in list_courses]
for line in myfile1:
list_courses.append(line.strip().split(','))
#print ("Gathering course information from file: \n",list_courses)
list_student=[]
with open("students.txt",'r') as myfile1:
# list_student=myfile1.readlines()
# list_student=[x.strip() for x in list_student]
for line in myfile1:
list_student.append(line.strip().split(','))
#print("Here is student info: \n",list_student)
list_final=[]
x=[]
x=list_student
for index,student in enumerate(list_student):
# print(index,student[0])
semester=student[0]
roll_number=(student[1])
print (("semester=%s roll_number=%s")%(semester,roll_number))
### if statements for cs students ###
if semester=='1' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
print ("course = ",course[0])
if course[0].startswith('cs1'):
# list_final=list_student[index]+list_courses[index2]
print (("semester=%s roll_number=%s")%(semester,roll_number))
x[index].extend(list_courses[index2])
if semester=='2' and roll_number.startswith('cs'):
for index2,course in enumerate (list_courses):
if course[0].startswith('cs1'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='3' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs2'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='4' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs2'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='5' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs3'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='6' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs3'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='7' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs4'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='8' and roll_number.startswith('cs') :
for index2,course in enumerate (list_courses):
if course[0].startswith('cs4'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
### if statements for ec students ####
if semester=='1' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
print ("course = ",course[0])
if course[0].startswith('ec1'):
# list_final=list_student[index]+list_courses[index2]
print (("semester=%s roll_number=%s")%(semester,roll_number))
x[index].extend(list_courses[index2])
if semester=='2' and roll_number.startswith('ec'):
for index2,course in enumerate (list_courses):
if course[0].startswith('ec1'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='3' and roll_number.startswith('ec') :
print("entered here")
for index2,course in enumerate (list_courses):
if course[0].startswith('ec2'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='4' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
if course[0].startswith('ec2'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='5' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
if course[0].startswith('ec3'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='6' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
if course[0].startswith('ec3'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='7' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
if course[0].startswith('ec4'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
if semester=='8' and roll_number.startswith('ec') :
for index2,course in enumerate (list_courses):
if course[0].startswith('ec4'):
# list_final=list_student[index]+list_courses[index2]
x[index].extend(list_courses[index2])
#print (list_final)
print (x)
答案 1 :(得分:0)
根据您的代码进行一些调整:
#course registration
list_courses=[]
with open("courses.txt",'r') as myfile1:
for line in myfile1:
list_courses.append(line.strip().split(','))
list_student=[]
with open("students.txt",'r') as myfile1:
for line in myfile1:
list_student.append(line.strip().split(','))
list_final=[]
for semester, roll_num, name in list_student:
faculty = roll_num[:2]
temp_result = [semester, roll_num, name]
course_lvl = faculty + semester
for course_num, course_name in list_courses:
if course_num.startswith(course_lvl):
temp_result.extend([course_num, course_name])
# omitting the students with no courses (comment if that's not what you want)
if len(temp_result) > 3:
list_final.append(temp_result)
# including students with no courses (uncomment if that's what you want)
# list_final.append(temp_result)
print (list_final)
from math import ceil
list_final=[]
for semester, roll_num, name in list_student:
faculty = roll_num[:2]
temp_result = [semester, roll_num, name]
course_lvl = faculty + str(ceil(int(semester) / 2))
for course_num, course_name in list_courses:
if course_num.startswith(course_lvl):
temp_result.extend([course_num, course_name])
list_final.append(temp_result)
print (list_final)