Created
December 1, 2016 22:58
-
-
Save looneym/9fcaa206ceb4c19a3dec2cbc74db2c0f to your computer and use it in GitHub Desktop.
Simple example showing list comprehensions in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car: | |
cars = [] | |
def __init__(self, num, cheap, red, broken): | |
self.num = num | |
self.cheap = cheap | |
self.red = red | |
self.broken = broken | |
Car.cars.append(self) | |
def cheap(c): | |
return c.cheap | |
def red(c): | |
return c.red | |
def broken(c): | |
return c.broken | |
car1 = Car(1,True,True,False) | |
car2 = Car(2,True,False,True) | |
car3 = Car(3,False,True,False) | |
approved_cars = [] | |
[approved_cars.append(c) for c in Car.cars if cheap(c) and red(c) and not broken(c)] | |
# prints 1 | |
for car in approved_cars: | |
print car.num | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment