Python:循环后输出错误

发布于 2025-01-12 10:35:55 字数 1810 浏览 0 评论 0原文

我有两个列表:

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.

image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2025-01-19 10:35:55

您正在迭代 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

不即不离 2025-01-19 10:35:55

文本 MATH 2050 在第一章和第二章中被提及两次。第二。因此,嵌套的 for 循环可以获得 2 X 2 = 4 条匹配记录。

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

我没有得到你正在做的事情的完整上下文,但我猜你想做以下


>>> 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')]

提示:

  1. 检查字符串函数,如 .startswith
  2. 检查 python 正则表达式

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文