我想知道是否有一种方法可以让 python 2.7 按类中的字符串按字母顺序对由类组成的列表进行排序。
class person: #set up for patient
def __init__(self, FName, LName):
self.FName = FName # the first name of patient
self.LName = LName # last name of patient
patients=person(raw_input('first name'),raw_input('second name'))
i=1
all=[patients]
orderAlphabet=[patients]
orderInjury=[patients]
print a[0]
while i<3:
patients=person(raw_input('first name'),raw_input('second name'))
a.append(patients)
i = i+1
在此示例中,我尝试按姓氏排序。
最佳答案
假设您想按患者的姓氏对患者进行排序。
类代码为:
class person: #set up for patient
def __init__(self, FName, LName):
self.FName = FName # the first name of patient
self.LName = LName # last name of patient
您可以使用以下方式获取用户的输入:
i = 1
patients=person(raw_input('first name'),raw_input('second name'))
a=[patients] #You have used 'all'
while(i < 3):
patients=person(raw_input('first name'),raw_input('second name'))
a.append(patients) #And you have used 'a' here.
i = i + 1
要按姓氏对患者进行排序,您可以使用:
orderAlphabet=[a[i].LName for i in range(0,3)]
orderAlphabet = orderAlphabet.sort()
关于python - 根据类 python 2.7 中的变量按字母顺序对类列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31553507/