python - 根据类 python 2.7 中的变量按字母顺序对类列表进行排序

标签 python list class python-2.7 sorting

我想知道是否有一种方法可以让 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/

相关文章:

c++ - 常量字符串如何在 C++ 中工作

javascript - 从 Javascript 访问类方法

python - odoo 10 模块名称无效

java - Scala 访问列表对象并评估它们

python - 根据数字和组 ID 扩展 pandas 数据框行 (Python 3)。

list - 您将如何遍历O(n ^ 0.5)中的链表?

c++ - 将 std::set 保存在 std::list 中

class - Grails新域实例返回null

Python Sorted() 跳过具有空列的行

python - 两个方括号并排 : strange effect