【问题标题】:Python - generate variable list comprehension expression based on inputPython - 根据输入生成变量列表理解表达式
【发布时间】:2019-07-04 11:56:11
【问题描述】:

这里是新手,是 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 网格(例如,# of unique LegPosts = len(heightList) * len(thickList) = 84)。为此,我创建了一个类 Part:

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

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

我被困在哪里:

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

谢谢

【问题讨论】:

  • 欢迎来到 StackOverflow。不幸的是,您的问题并不清楚。 “生成不同的列表理解表达式”是指您想要获得所显示的理解的 result,还是想要一个包含所显示的理解的字符串,或者其他什么? lengthListdepthListthickList 的功能是什么?等等。如果您向我们展示一个显示您想要的内容的完整示例,这将有所帮助——包括输入和所需的输出。阅读How to create a Minimal, Complete, and Verifiable example

标签: python generator list-comprehension


【解决方案1】:
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

【讨论】:

  • *args 表示法有点帮助,但我仍然不确定如何从 n 级嵌套 for 循环中获取有序元组列表?
【解决方案2】:

这就是 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

【讨论】:

    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 2015-12-18
    • 2021-11-12
    • 2014-07-19
    • 2015-01-01
    • 2016-10-04
    • 2011-01-01
    • 1970-01-01
    相关资源
    最近更新 更多