Python 对象列表,获取所有 'orientations' 属性
Python list of objects, get all 'orientations' of attributes
我有一个对象列表(矩形)。每个对象都有 2 个属性(高度和宽度)。我想获得此列表的所有 'orientations'(不确定如何准确称呼它),因此所有 2^n(对于 n 个矩形的列表)方向,其中矩形的高度和宽度(可能)交换。对于 3 个对象的列表,它看起来像这样(顺序不重要):
[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 看起来像这样:
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"""
有没有简单的方法来做到这一点?
这里有一些 Python 代码可以满足您的需求。如果您修复 show()
函数中的打印语句,它应该很容易在 Python 3 上 运行。
#!/usr/bin/env python
''' Build a list of lists containing all combinations of orientations
(i.e. landscape & portrait) for a list of Rectangle objects
From
Written by PM 2Ring 2015.05.02
'''
from itertools import product
#A simple rectangle class
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
def __repr__(self):
return 'Rectangle({0}, {1})'.format(self.width, self.height)
def transpose(self):
return Rectangle(self.height, self.width)
#Helper function to print sequences
def show(seq):
for item in seq:
print item
print
#A list of rectangle objects
rects_orig = [
Rectangle(1, 2),
Rectangle(3, 4),
Rectangle(5, 6),
]
show(rects_orig)
#The transposed versions of those rectangles
rects_rot = [rect.transpose() for rect in rects_orig]
show(rects_rot)
#Join both lists into a list of tuples
rects_both = zip(rects_orig, rects_rot)
show(rects_both)
#Build the combinations.
combos = []
for align in product([0, 1], repeat=len(rects_both)):
combos.append([rect_pair[a] for a, rect_pair in zip(align, rects_both)])
show(combos)
输出
Rectangle(1, 2)
Rectangle(3, 4)
Rectangle(5, 6)
Rectangle(2, 1)
Rectangle(4, 3)
Rectangle(6, 5)
(Rectangle(1, 2), Rectangle(2, 1))
(Rectangle(3, 4), Rectangle(4, 3))
(Rectangle(5, 6), Rectangle(6, 5))
[Rectangle(1, 2), Rectangle(3, 4), Rectangle(5, 6)]
[Rectangle(1, 2), Rectangle(3, 4), Rectangle(6, 5)]
[Rectangle(1, 2), Rectangle(4, 3), Rectangle(5, 6)]
[Rectangle(1, 2), Rectangle(4, 3), Rectangle(6, 5)]
[Rectangle(2, 1), Rectangle(3, 4), Rectangle(5, 6)]
[Rectangle(2, 1), Rectangle(3, 4), Rectangle(6, 5)]
[Rectangle(2, 1), Rectangle(4, 3), Rectangle(5, 6)]
[Rectangle(2, 1), Rectangle(4, 3), Rectangle(6, 5)]
准备:
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))
我有一个对象列表(矩形)。每个对象都有 2 个属性(高度和宽度)。我想获得此列表的所有 'orientations'(不确定如何准确称呼它),因此所有 2^n(对于 n 个矩形的列表)方向,其中矩形的高度和宽度(可能)交换。对于 3 个对象的列表,它看起来像这样(顺序不重要):
[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 看起来像这样:
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"""
有没有简单的方法来做到这一点?
这里有一些 Python 代码可以满足您的需求。如果您修复 show()
函数中的打印语句,它应该很容易在 Python 3 上 运行。
#!/usr/bin/env python
''' Build a list of lists containing all combinations of orientations
(i.e. landscape & portrait) for a list of Rectangle objects
From
Written by PM 2Ring 2015.05.02
'''
from itertools import product
#A simple rectangle class
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
def __repr__(self):
return 'Rectangle({0}, {1})'.format(self.width, self.height)
def transpose(self):
return Rectangle(self.height, self.width)
#Helper function to print sequences
def show(seq):
for item in seq:
print item
print
#A list of rectangle objects
rects_orig = [
Rectangle(1, 2),
Rectangle(3, 4),
Rectangle(5, 6),
]
show(rects_orig)
#The transposed versions of those rectangles
rects_rot = [rect.transpose() for rect in rects_orig]
show(rects_rot)
#Join both lists into a list of tuples
rects_both = zip(rects_orig, rects_rot)
show(rects_both)
#Build the combinations.
combos = []
for align in product([0, 1], repeat=len(rects_both)):
combos.append([rect_pair[a] for a, rect_pair in zip(align, rects_both)])
show(combos)
输出
Rectangle(1, 2)
Rectangle(3, 4)
Rectangle(5, 6)
Rectangle(2, 1)
Rectangle(4, 3)
Rectangle(6, 5)
(Rectangle(1, 2), Rectangle(2, 1))
(Rectangle(3, 4), Rectangle(4, 3))
(Rectangle(5, 6), Rectangle(6, 5))
[Rectangle(1, 2), Rectangle(3, 4), Rectangle(5, 6)]
[Rectangle(1, 2), Rectangle(3, 4), Rectangle(6, 5)]
[Rectangle(1, 2), Rectangle(4, 3), Rectangle(5, 6)]
[Rectangle(1, 2), Rectangle(4, 3), Rectangle(6, 5)]
[Rectangle(2, 1), Rectangle(3, 4), Rectangle(5, 6)]
[Rectangle(2, 1), Rectangle(3, 4), Rectangle(6, 5)]
[Rectangle(2, 1), Rectangle(4, 3), Rectangle(5, 6)]
[Rectangle(2, 1), Rectangle(4, 3), Rectangle(6, 5)]
准备:
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))