Skip to content

Instantly share code, notes, and snippets.

@thomwolf
Last active June 11, 2018 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomwolf/0709b5a72cf3620cd00d94791213d38e to your computer and use it in GitHub Desktop.
Save thomwolf/0709b5a72cf3620cd00d94791213d38e to your computer and use it in GitHub Desktop.
A Python loop on a list of Python objects
from random import random
class Rectangle:
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
def check_rectangles(rectangles, threshold):
n_out = 0
for rectangle in rectangles:
if rectangle.area() > threshold:
n_out += 1
return n_out
def main():
n_rectangles = 10000000
rectangles = list(Rectangle(random(), random()) for i in range(n_rectangles))
n_out = check_rectangles(rectangles, threshold=0.25)
print(n_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment