Python-根据输入生成变量列表理解表达式

时间:2019-02-11 01:38:35

标签: python generator list-comprehension

这是Python的新功能,所以请保持警惕

我正在尝试定义一个函数,以基于输入生成不同的列表理解表达式。我想发生的事情:

输入:

TableTop = Part(L,D,T)

生成:

TableTop.unique = [(L,D,T) for L in lengthList for D in depthlist, for T in thickList]

或输入:

LegPost = Part(H,T)

生成:

LegPost.unique = [(H,T) for H in heightList for T in thickList]

编辑

对不起,让我们再试一次。

上下文:

我正在尝试编写一个程序以接收“零件”列表(在本例中为厨房桌子的零件)和“尺寸”列表(用于自定义桌子尺寸的选项),并输出Blender网格对象。这张桌子有9个零件和5个尺寸

partList = (TableTop, LegPost, BaseFoot, AngleBrace, CenterAngle, TopCap, HorizontalBrace, VerticalBrace, CenterBrace)

dimList = (lengthList, depthList, heightList, thickList, ohangList)

lengthList = range(60,145)   # table length options (60"-144")
depthList = range(30,55)     # table depth options (30"-54")
heightList = range(30,45)    # table height options (30"-44")
thickList = range(3,9)       # tabletop thickness options (3/4"-2")
ohangList = range(10,23)     # table end overhang options (10"-22")

表的每个部分均不受每个维度的影响。我想为每个 unique 表部件创建一个Blender网格(例如,唯一的LegPosts的数量= len(heightList) * len(thickList) = 84)。为此,我创建了一个类Part:

class Part:  
    def __init__(self, dims):  
        self.dims = dims
        ...

我想尽可能抽象地定义此类,以便以后可以用于其他家具和其他类型的家具。

我被困在哪里:

我想在类中定义一个在实例化时运行的函数,接受一个元组(例如(L,D,T)),并生成列表推导(例如,lengthList中的L的[[L,D,T) [对于depthlist中的D,thickList中的T]),其中L,D,H,T和G始终分别对应于lengthList,depthList,heightList,thickList和ohangList。我不知道如何创建此功能,只是在寻找一些方向。

谢谢

2 个答案:

答案 0 :(得分:0)

class Part:  
    def __init__(self, *dims):  
        self.dims = *dims
        ...

self.dims打开*dims的包装后,就可以对其进行for循环以完成您需要的操作。要执行您需要的操作,您将需要使用递归来生成n级嵌套for循环

def gen_nestedforloop(dims): #throw in self.dims here
    output = []
    if len(dims) > 1:
        gen_nestedforloop(dims[1:])
    else:
        output.append([x for x in dims])
        return output

答案 1 :(得分:0)

这就是itertools.product存在的原因:

import itertools

L = range(60,145)   # table length options (60"-144")
D = range(30,55)     # table depth options (30"-54")
H = range(30,45)    # table height options (30"-44")
T = range(3,9)       # tabletop thickness options (3/4"-2")
O = range(10,23)     # table end overhang options (10"-22")

class Part:
    def __init__(self, dims):
        self._dims = dims

    @property
    def unique(self):
        return list(itertools.product(*self._dims))

>>> Part((T,H)).unique
[(3, 30), (3, 31), (3, 32), (3, 33), (3, 34), ..., (8, 39), (8, 40), (8, 41), (8, 42), (8, 43), (8, 44)]

如果要创建自己的实现,则应使用递归函数:

>>> def cprod(*dims):
...     if len(dims) == 0: return []
...     if len(dims) == 1: return list(dims[0])
...     def cprod_aux(d, *ds):
...         return [(x, *y) for x in d for y in cprod_aux(*ds)] if ds else ((x,) for x in d)
...     return cprod_aux(*dims)
>>> cprod()
[]
>>> cprod(D)
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54]
>>> cprod(L, D, H)
[(60, 30, 30), (60, 30, 31), (60, 30, 32), (60, 30, 33), (60, 30, 34), (60, 30, 35), ..., (144, 54, 39), (144, 54, 40), (144, 54, 41), (144, 54, 42), (144, 54, 43), (144, 54, 44)]

实施过于幼稚(您可以使用一些memoization