﻿ Python对象列表,获取属性的所有“方向” - 代码日志

#### Python对象列表,获取属性的所有“方向”

``````[R1(w, h), R2(w2, h2), R3(w3, h3)]
[R1(w, h), R2(w2, h2), R3(h3, w3)]
[R1(w, h), R2(h2, w2), R3(w3, h3)]
[R1(w, h), R2(h2, w2), R3(h3, w3)]
[R1(h, w), R2(w2, h2), R3(w3, h3)]
[R1(h, w), R2(w2, h2), R3(h3, w3)]
[R1(h, w), R2(h2, w2), R3(w3, h3)]
[R1(h, w), R2(h2, w2), R3(h3, w3)]
``````

``````class Rectangle:
def __init__(self, height, width):
self.height = height
self.width = width

def place(self):
"""Method to place tile in a larger grid"""

def remove(self):
"""Method to remove tile from larger grid"""
``````

``````class Rectangle:
def __init__(self, height, width):
self.height = height
self.width = width

def flipped(self):
return Rectangle(self.width, self.height)

def __repr__(self):
return 'Rectangle({}, {})'.format(self.height, self.width)

rectangles = [Rectangle(1, 10), Rectangle(2, 20), Rectangle(3, 30)]
``````

``````from itertools import product
for orientation in product(*zip(rectangles, map(Rectangle.flipped, rectangles))):
print(orientation)
``````

``````(Rectangle(1, 10), Rectangle(2, 20), Rectangle(3, 30))
(Rectangle(1, 10), Rectangle(2, 20), Rectangle(30, 3))
(Rectangle(1, 10), Rectangle(20, 2), Rectangle(3, 30))
(Rectangle(1, 10), Rectangle(20, 2), Rectangle(30, 3))
(Rectangle(10, 1), Rectangle(2, 20), Rectangle(3, 30))
(Rectangle(10, 1), Rectangle(2, 20), Rectangle(30, 3))
(Rectangle(10, 1), Rectangle(20, 2), Rectangle(3, 30))
(Rectangle(10, 1), Rectangle(20, 2), Rectangle(30, 3))
``````