简体   繁体   中英

What's the most Pythonic way to refactor this list building code?

I have a list of results that I need to pull out various other lists from. For example, all owners that are male, all cars between 5 and 10 years old.

def get_male_owners(self):
    results = []
    for result in self.results:
        if result.owner.sex.male:
            results.append(result)
    return results

def get_cars_5_to_10(self):
    results = []
    for result in self.results:
        if result.car:
            if self.is_5_to_10(result.car):
                results.append(result)
    return results

def is_5_to_10(self, car):
    if car.age <= 10 and car.age >= 5:
        return True
    else:
        return False

The thing is there will be lots of different lists I need to build, but a lot of the code in each of the list building functions is common. What's the best way to implement DRY in a Pythonic way here? Thanks.

Use list comprehensions:

def get_male_owners(self):
    return [res for res in self.results if res.owner.sex.male]

def get_cards_5_to_10(self):
    return [res for res in self.results if res.car and self.is_5_to_10(res.car)]

def is_5_to_10(self, car):
    return 5 <= car.age <= 10

If you just need something iterable you could also return a generator expression by replacing the brackets with parentheses.

And yes, the x <= y <= z expression does work in python and it does yield the correct result and not something like (5 <= car.age) <= 10 .

def filter(self, by=None):
    return [res for res in self.results if by(res)]

def get_male_owners(self):
    return self.filter(lambda res: res.owner.sex.male)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM