Python:循环后输出错误
我有两个列表:
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']
我
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']
在它们上运行这个循环,它应该检查在coursestaken中的课程是否在courses_grade_final中具有成绩,并按顺序将它们一起返回,如果课程没有成绩又名(正在进行中)它跳过它:-
allcourses = []
for x in coursestaken:
for y in course_grade_final:
if x in y:
allcourses.append(x)
allcourses.append(y[-2:])
else:
continue
输出是:
['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']
问题是 MATH 2050 由于某种原因加倍,我不明白为什么,正确的列表只有两个 MATH 2050,一个后面跟着 44,一个后面跟着83.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在迭代 MATH 2050 的每个实例的整个列表,因此两次都会找到两个结果,结果都是 4。
您可能只想删除课程中多余的“数学 2050”
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
文本
MATH 2050
在第一章和第二章中被提及两次。第二。因此,嵌套的 for 循环可以获得 2 X 2 = 4 条匹配记录。我没有得到你正在做的事情的完整上下文,但我猜你想做以下
提示:
.startswith
Text
MATH 2050
is mentioned twice in first & second. So a nested for loops get you 2 X 2 = 4 matching records.I did not get completed context of what you are doing but I am guessing you wanted to do the following
Tips:
.startswith