class Student:
def __init__(self, name, major, gpa, onProbation):
self.name = name
self.major = major
self.gpa = gpa
self.onProbation = onProbation
Student1 = Student("Josh", "Business", 3.8, False)
Student2 = Student("Maya", "Accountancy", 2.5, True)
Student3 = Student("Dan", "Psychology", 1.2, True)
Student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
Student5 = Student("Michelle", "Medicine", 3.7, False)
Student6 = Student("Joey", "Law", 4.0, False)
Students = ["Josh", "Maya", "Dan", "Keon", "Michelle", "Joey"]
我想弄清楚如何從串列中洗掉所有處于試用期的學生,所以如果我輸入 print(Students),它只會給我未處于試用期的學生(Josh、Keon、Michelle 和 Joey )
uj5u.com熱心網友回復:
我相信我已經找到了你問題的答案。我還想指出,最好不要在init方法中創建學生物件,將所有學生存盤在串列中很有用。它是:
class Student:
def __init__(self, name, major, gpa, onProbation):
self.name = name
self.major = major
self.gpa = gpa
self.onProbation = onProbation
students = [
Student("Josh", "Business", 3.8, False),
Student("Maya", "Accountancy", 2.5, True),
Student("Dan", "Psychology", 1.2, True),
Student("Keon", "Biomedical Engineering", 4.0, False),
Student("Michelle", "Medicine", 3.7, False),
Student("Joey", "Law", 4.0, False)
]
# I created a list for new students because it will mess up the for loop if you remove objects from a list while iterating
new_students = []
for student in students:
if student.onProbation == False:
new_students.append(student)
# print(students) will return an unreadable list with encoded numbers and such so I did this instead
for student in new_students:
print(student.name, student.onProbation)
我希望這是有用的。
uj5u.com熱心網友回復:
class Student:
def __init__(self, name, major, gpa, onProbation):
self.name = name
self.major = major
self.gpa = gpa
self.onProbation = onProbation
@staticmethod
def getStudentsNotOnProbation(students):
res = []
for student in students:
if not student.onProbation:
res.append(student)
return res
Student1 = Student("Josh", "Business", 3.8, False)
Student2 = Student("Maya", "Accountancy", 2.5, True)
Student3 = Student("Dan", "Psychology", 1.2, True)
Student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
Student5 = Student("Michelle", "Medicine", 3.7, False)
Student6 = Student("Joey", "Law", 4.0, False)
Students = [Student1, Student2, Student3, Student4, Student5, Student6]
print(Student.getStudentsNotOnProbation(Students))
我使用 staticmethod 的原因是邏輯應該封裝在 Student 類本身中。
uj5u.com熱心網友回復:
我就是這樣做的。當你創建類時,它甚至會記住串列中的屬性分配。cpython中有個叫pylist的
class Student:
def __init__(self, name: str, major: str, gpa:float, onProbation: bool):
self.name = name
self.major = major
self.gpa = gpa
self.onProbation = onProbation
# in your example you placed the class instances in the class,
# notice how mine are placed outside. The __init__ method initializes
# the object, having it within the object is infinitly recursive i believe
student1 = Student("Josh", "Business", 3.8, False)
student2 = Student("Maya", "Accountancy", 2.5, True)
student3 = Student("Dan", "Psychology", 1.2, True)
student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
student5 = Student("Michelle", "Medicine", 3.7, False)
student6 = Student("Joey", "Law", 4.0, False)
students = [student1,student2,student3,student4,student5, student6]
mylist = students
for index, student in enumerate(students):
if student.onProbation: # if the boolean isnt specified, defaults to true as is
mylist.remove(students[index])
正如約翰戈登指出的那樣,你可以使用串列理解
class Student:
def __init__(self, name: str, major: str, gpa:float, onProbation: bool):
self.name = name
self.major = major
self.gpa = gpa
self.onProbation = onProbation
student1 = Student("Josh", "Business", 3.8, False)
student2 = Student("Maya", "Accountancy", 2.5, True)
student3 = Student("Dan", "Psychology", 1.2, True)
student4 = Student("Keon", "Biomedical Engineering", 4.0, False)
student5 = Student("Michelle", "Medicine", 3.7, False)
student6 = Student("Joey", "Law", 4.0, False)
students = [student1,student2,student3,student4,student5, student6]
good_students = [x for x in students if not student.onProbation]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/536997.html
上一篇:怎么修?TypeError:“PasswordManager”型別的引數不可迭代
下一篇:PythonOOP類方法書