Skip to content Skip to sidebar Skip to footer

Combining Two List Into Third Based On Condition

This is python 3 code, there are 2 input files courses.txt and student.txt I am able to read the course information from courses.txt in a list and student information in another l

Solution 1:

Based on your code just some tweaks:

#course registration
list_courses=[]
withopen("courses.txt",'r') as myfile1:
    for line in myfile1:
        list_courses.append(line.strip().split(','))


list_student=[]
withopen("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)iflen(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)

After I understood better the OP question:

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)

Solution 2:

Here is how I finally got the desired result.But any suggestions that can reduce the multiple if statements in my answer are welcome and also acceptable. So if there is a better way of doing please answer.

#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
forindex,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)

Post a Comment for "Combining Two List Into Third Based On Condition"