繁体   English   中英

嵌套循环确定两个班级的学生(两者)

[英]Nested loops to determine students in two classes (both)

我必须使用嵌套循环进行此练习。

我有两个班级名单,需要确定哪两个班级的学生。 我已经编写了代码并且它可以工作但是现在我的for循环再次迭代所有学生并打印结果与迭代相同而不仅仅是实际学生(在两个类中)。 如果我让cs_students中的学生与maths_students中的学生一致,但它不是嵌套的,那么它确实有效。

我的代码如下:

math_students = ["Audrey", "Ben", "Julia", "Paul", "Gerry", "Sue",
             "Helena", "Harry", "Marco", "Rachel", "Tina", "Mark", "Jackson"]
cs_students = ["William", "Aroha", "Melissa", "Sue", "Ben", "Audrey", "Susan", "Mark", "Hemi", "Brendan", "Paul", "Barry", "Julia"]


for student in math_students:
    count = 0
    for student in cs_students:
        if student in math_students:
        count +=1
        print("Student:", student, "is enrolled in both classes")
print(count, "Students are enrolled in Computer Science and Maths")

我的输出是:

Student: Sue is enrolled in both classes
Student: Ben is enrolled in both classes
Student: Audrey is enrolled in both classes
Student: Mark is enrolled in both classes
Student: Paul is enrolled in both classes
Student: Julia is enrolled in both classes
Student: Sue is enrolled in both classes
Student: Ben is enrolled in both classes
Student: Audrey is enrolled in both classes
Student: Mark is enrolled in both classes
Student: Paul is enrolled in both classes
Student: Julia is enrolled in both classes and so on.......
6 Students are enrolled in Computer Science and Maths

我想你正在寻找这样的东西:

in_both_classes = []
for math_student in math_students:
    for cs_student in cs_students:
        if math_student == cs_student:
            in_both_classes.append(math_student)

print "There are {} students in both classes: {}".format(
    len(in_both_classes),
    ", ".join(in_both_classes),
)

你在两个for循环中使用了相同的变量student 这样做可以防止您将列表值相互比较。

那个,简单的if语句可以解决这个问题:

math_students = ["Audrey", "Ben", "Julia", "Paul", "Gerry", "Sue",
             "Helena", "Harry", "Marco", "Rachel", "Tina", "Mark", "Jackson"]
cs_students = ["William", "Aroha", "Melissa", "Sue", "Ben",
             "Audrey","Susan", "Mark", "Hemi", "Brendan", "Paul", "Barry", "Julia"]

count = 0
for student1 in math_students:     #change this to student1
    for student2 in cs_students:   #change this to student2
        if student1 ==student2:    #check if student1 is equal to student 2
            count +=1
            print("Student:", student2, "is enrolled in both classes") #print student2
print(count, "Students are enrolled in Computer Science and Maths")

现在这将输出:

>>>
('Student:', 'Audrey', 'is enrolled in both classes')
('Student:', 'Ben', 'is enrolled in both classes')
('Student:', 'Julia', 'is enrolled in both classes')
('Student:', 'Paul', 'is enrolled in both classes')
('Student:', 'Sue', 'is enrolled in both classes')
('Student:', 'Mark', 'is enrolled in both classes')
(6, 'Students are enrolled in Computer Science and Maths')

当您检索两个列表时,使用相同的变量名称studnet ,您应该重命名它们中的任何一个,例如s1s2并且仅当s1等于s2时才增加计数。

顺便说一句,我有另一个建议,你可以使用Sets Intersection

math_students = ["Audrey", "Ben", "Julia", "Paul", "Gerry", "Sue",
             "Helena", "Harry", "Marco", "Rachel", "Tina", "Mark", "Jackson"]
cs_students = ["William", "Aroha", "Melissa", "Sue", "Ben", "Audrey", "Susan", "Mark", "Hemi", "Brendan", "Paul", "Barry", "Julia"]

math_set = set(math_students)
cs_set = set(cs_students)

# students in both cs and math
common_set = math_set & cs_set

#the studnets only in cs
only_cs = cs_set - math_set

# the students only in math
only_math = math_set - cs_set


print common_set
print "----"
print only_cs
print "----"
print only_math

使用列表推导来过滤数学类和计算机科学类中的人:

# contains only the students in both original lists
both = [i for i in math_students if i in cs_students]

for student in both:
    # interpolating the name of each student in both list
    print '{} is enrolled in both classes'.format(student)

# interpolating the number of students in the both list
print '{} students are in both classes'.format(len(both))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM