Python中的奇怪输出

时间:2015-02-28 18:53:55

标签: python python-3.4

我正在生成一份员工和经理名单。但是,在尝试按姓氏排序之后,我获得了这个奇怪的输出(我的初始类员工只包含“名称”但不分为名字和姓氏。然后,我使用[1]来表示姓氏)。我的代码有什么问题,因为我看不到我的员工名单。

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

employeeList = []
employee1 = Employee("Banny Chu", "777-88-9999", 45000)
employee2 = Employee("Luffy Monkey", "555-66-9999", 32000)
employee3 = Employee("Zoro Nonoroa", "222-00-3333", 37000)
manager1 = Manager("Scalt Haight", "444-33-1111", 60000, "Lab", 2300)
manager2 = Manager("Kapu Ro", "333-44-2222", 65000, "General", 2600)
manager3 = Manager("Nami Swan", "111-77-6666", 80000, "HR", 3000)
employeeList.append(employee1)
employeeList.append(employee2)
employeeList.append(employee3)
employeeList.append(manager1)
employeeList.append(manager2)
employeeList.append(manager3)

print (sorted(employeeList, key=lambda employee: employee.name[1].lower()))

输出如下(奇怪的输出,因为即使我输入print(employeeList)并且给出了与下面相同的格式,我也无法以正确的格式看到我的employeeList。

[<employee8.Employee object at 0x105a48b00>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>, <manager8.Manager object at 0x1054290f0>]

我应该如何修改它,以便能够以我能清楚看到的方式查看我的排序列表?

3 个答案:

答案 0 :(得分:2)

默认情况下,用户定义的对象将表示为内存中某个位置的类实例:

<__main__.Employee instance at 0x02A39940>

您需要在Employee类中添加一个特殊的对象表示方法:

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

    def __repr__(self):
        return self.name # represents object with name

答案 1 :(得分:0)

您错过了排序点返回根据您发送的条件排序的列表排列的点。如果这是您所期望的,那么它不会自动返回您对它们进行排序的键吗?

sort = sorted(employeeList, key=lambda employee: employee.name[1].lower())
print([emp.name.split()[1] for emp in employeeList])

输出(我很懒,只复制粘贴了3名员工):

['Chu', 'Monkey', 'Nonoroa']

您也错过了分割,因为您将名称保存在单个字符串中。索引单个字符串将在字符串中的该位置返回单个字符。

如果您的目标不是打印姓氏,那么您必须覆盖__str____repr__方法。 (了解方法here之间的确切差异。)

答案 2 :(得分:0)

您忘记在关键功能中分割名称:employee.name.split(' ')[1]。 Python在排序列表上调用__repr__,打印出&#39; [&#39;和&#39;]&#39;在开头和结尾,然后在每个列表元素上调用__repr__。默认__repr__打印对象类型和地址。如果你想看到别的东西,你必须给python另一个__repr__来打电话。

class Employee:
    def __init__(self, name, socialSecurityNumber, salary):
        """
        Set name, socialSecurityNumber, and salary to itself.
        """
        self.name = name
        self.socialSecurityNumber = socialSecurityNumber
        self.salary = salary

    def __repr__(self):
        return "{} {} {}".format(self.name, self.socialSecurityNumber, self.salary)

class Manager(Employee):
    def __init__(self, name, socialSecurityNumber, salary, department, unknown):
        super().__init__(name, socialSecurityNumber, salary)



employeeList = []
employee1 = Employee("Banny Chu", "777-88-9999", 45000)
employee2 = Employee("Luffy Monkey", "555-66-9999", 32000)
employee3 = Employee("Zoro Nonoroa", "222-00-3333", 37000)
manager1 = Manager("Scalt Haight", "444-33-1111", 60000, "Lab", 2300)
manager2 = Manager("Kapu Ro", "333-44-2222", 65000, "General", 2600)
manager3 = Manager("Nami Swan", "111-77-6666", 80000, "HR", 3000)
employeeList.append(employee1)
employeeList.append(employee2)
employeeList.append(employee3)
employeeList.append(manager1)
employeeList.append(manager2)
employeeList.append(manager3)

for employee in sorted(employeeList, key=lambda employee: employee.name.split(' ')[1].lower()):
    print(employee)