Home > database >  Python: wrong output after loop
Python: wrong output after loop

Time:03-08

I have two lists:

coursestaken = ['COMP 1002', 'ECON 1020', 'ENGL 1110', 'MATH 1001', 'MATH 2050', 'BUSI 1101', 'MATH 2000', 'MATH 2050', 'OCSC 1000', 'STAT 2500', 'BUSI 1210', 'BUSI 1600', 'BUSI 3310', 'COMP 1003', 'BUSI 1000', 'COMP 1001', 'ECON 1010', 'ENGL 1090', 'MATH 1000']

and

course_grade_final = ['COMP 1002 Intro to Logic for Comp Sci B 65', 'ECON 1020 Intro to Macroeconomics A 89', 'ENGL 1110 Crtcl Rdng & Wrtng in Rhetori C 60', 'MATH 1001 Calculus II B 67', 'MATH 2050 Linear Algebra I F 44', 'BUSI 1101 Principles of Accounting A 89', 'MATH 2000 Calculus III A 80', 'MATH 2050 Linear Algebra I A 83', 'OCSC 1000 Exploration of World Ocean A 89', 'STAT 2500 Stats/Busi&Art Students I B 77', 'BUSI 1000 Intro to Business in Society B 78', 'COMP 1001 Intro to Programming B 69', 'ECON 1010 Intro to Microeconomics A 88', 'ENGL 1090 CRW: Telling Stories C 62', 'MATH 1000 Calculus I B 68']

I run this loop on them which is supposed to check if a course in coursestaken has a grade in courses_grade_final and return them together in order, if a course doesn't have a grade aka (its in progress) it skips it:-

allcourses = []
for x in coursestaken:
    for y in course_grade_final:
        if x in y:
            allcourses.append(x)
            allcourses.append(y[-2:])
        else:
            continue

and the output is:

['COMP 1002', '65', 'ECON 1020', '89', 'ENGL 1110', '60', 'MATH 1001', '67', 'MATH 2050', '44', 'MATH 2050', '83', 'BUSI 1101', '89', 'MATH 2000', '80', 'MATH 2050', '44', 'MATH 2050', '83', 'OCSC 1000', '89', 'STAT 2500', '77', 'BUSI 1000', '78', 'COMP 1001', '69', 'ECON 1010', '88', 'ENGL 1090', '62', 'MATH 1000', '68'] 

the issue is MATH 2050 is doubled for some reason and I can't understand why, the correct list would only have two MATH 2050s, one followed by 44 and one followed by 83.

image

CodePudding user response:

You're iterating through the entire list for each instance of MATH 2050, so it will find both results both times resulting in four.

You probably just want to remove the extra 'MATH 2050' you have in coursestaken

CodePudding user response:

Text MATH 2050 is mentioned twice in first & second. So a nested for loops get you 2 X 2 = 4 matching records.

First List | Second List
-----------|------------------------------------
'MATH 2050'| 'MATH 2050 Linear Algebra I F 44')
'MATH 2050'| 'MATH 2050 Linear Algebra I A 83')

I did not get completed context of what you are doing but I am guessing you wanted to do the following


>>> for x, y in zip(coursestaken, course_grade_final):
...     print((x, y[-2:]))
...
('COMP 1002', '65')
('ECON 1020', '89')
('ENGL 1110', '60')
('MATH 1001', '67')
('MATH 2050', '44')
('BUSI 1101', '89')
('MATH 2000', '80')
('MATH 2050', '83')
('OCSC 1000', '89')
('STAT 2500', '77')
('BUSI 1210', '78')
('BUSI 1600', '69')
('BUSI 3310', '88')
('COMP 1003', '62')
('BUSI 1000', '68')

>>> for x, y in zip(coursestaken, course_grade_final):
...     allcourses.append((x, y[-2:]))
...
>>> allcourses
[('COMP 1002', '65'), ('ECON 1020', '89'), ('ENGL 1110', '60'), ('MATH 1001', '67'), ('MATH 2050', '44'), ('BUSI 1101', '89'), ('MATH 2000', '80'), ('MATH 2050', '83'), ('OCSC 1000', '89'), ('STAT 2500', '77'), ('BUSI 1210', '78'), ('BUSI 1600', '69'), ('BUSI 3310', '88'), ('COMP 1003', '62'), ('BUSI 1000', '68')]

Tips:

  1. Check string functions like .startswith
  2. Check python regular expressions
  • Related