带对象python的排序列表-更有效的方法?

时间:2018-11-06 13:10:19

标签: python class object

我正在用Python做一个初学者的任务,想知道是否有一种方法可以简化此代码,即不要在类中创建列表。需要按所有参数名称,年龄,物种和相反的顺序对列表进行排序。

所以这可行,但是很丑:

def sortAnimal(llist):
    while True:
        print("\nChoose which parameter to sort: \n")
        print("1. Name\n2. Age\n3. Species\n")
        choice = int(input("Choice:"))
        print("\n")

        if choice == 1:
            print("{:7s} {:7s} {:7s} {:7s}".format("Name:", "Age:", "Species:", "Gender:"))
            sortedList = sorted(llist, key=lambda animal: animal.name)
            for obj in sortedList:
                print(obj)

            if input("\To reverse the list press enter. Otherwise press m + enter") == "":
                print("\n")
                print("{:7s} {:7s} {:7s} {:7s}".format("Name:", "Age:", "Species:", "Gender:"))
                sortedList = sorted(llist, key=lambda animal: animal.name, reverse= True)
                for obj in sortedList:
                    print(obj)
            else:
                pass

        elif choice == 2:
            # same code as option 1 but with animal.age
            pass

        elif choice == 3:
            #same code as option 1 but with animal.species
            pass

        if input("\To sort again press enter, back to menu press m + enter") == "":
            continue
        else:
            break

我以为是这样的功能,但也没有用:

def sortPrintAnimal(parameter, llist):
    while True:
        sortedList = sorted(llist, key=lambda animal: animal.parameter)
        print("{:7s} {:7s} {:7s} {:7s}".format("Name:", "Age:", "Species:", "Gender:"))
        for obj in sortedList:
            print(obj)

        if input("If you want to reverse the list press enter. Back to menu press m + enter: ") == "":
            # reversing the list
            pass
        else:
            break

2 个答案:

答案 0 :(得分:2)

该如何使用 getattr(obj,attr)

def sortAnimals(animals, key='name'):
    return sorted(animals, key=lambda animal: getattr(animal,key))


def attr(choice):
    return {
        1: "name",
        2: "age",
        3: "species"
    }[choice]


def display_animals(animals):
    print("{:7s} {:7s} {:7s} {:7s}".format("Name:", "Age:", "Species:", "Gender:"))
    for obj in animals:
        print(obj)


def main(animals):
    while True:
        print("\nChoose which parameter to sort: \n")
        print("1. Name\n2. Age\n3. Species\n")
        choice = int(input("Choice:"))
        try:
            sorted_animals = sortAnimals(animals, attr(choice))
        except KeyError:
            print("Invalid Choice {}".format(choice))
            continue
        display_animals(sorted_animals)

        if not input("To reverse the list press enter. Otherwise press m + enter"):
            sorted_animals.reverse()
            display_animals(sorted_animals)

        if input("To sort again press enter, back to menu press m + enter"):
            break

main(animals_list)

正如您提到的有效方式一样,如果列表很大,则存储排序后的列表以供将来使用会有所帮助

from collections import defaultdict
def main(animals):
    sort_map = defaultdict(dict)

    def _sorted_map(key, r_flag=False):
        if key not in sort_map:
            sort_map[key][False] = sortAnimals(animals, key)
        if r_flag and r_flag not in sort_map[key]:
            sort_map[key][True] = list(reversed(sort_map[key][False]))
        return sort_map[key][r_flag]


    while True:

        print("\nChoose which parameter to sort: \n")
        print("1. Name\n2. Age\n3. Species\n")
        choice = int(input("Choice:"))

        try:
            animal_attr = attr(choice)
        except KeyError:
            print("Invalid Choice {}".format(choice))
            continue

        sorted_animals = _sorted_map(animal_attr)
        display_animals(sorted_animals)

        if not input("To reverse the list press enter. Otherwise press m + enter"):
            sorted_animals = _sorted_map(animal_attr, True)
            display_animals(sorted_animals)

        if input("To sort again press enter, back to menu press m + enter"):
            break

答案 1 :(得分:0)

您可以使用字典,其中是选项,而 values 是用作键的函数(lambda),例如:

sudo apt install ruby-full