【问题标题】:Python: Iterating over loopsPython:迭代循环
【发布时间】:2014-08-23 19:21:52
【问题描述】:

我无法针对任意长度的indeces 概括以下代码:

sol = 0
for f_2 in F_2:
        if f_2 in A[0]:
            fac1 = f(f_2,indeces[0])
            for f_1 in F_1:
                if f_1 in A[0][f_2][0]:
                    fac2 = f(f_1,indeces[1])
                    for f_0 in F_0:
                        if f_0 in A[0][f_2][0][f_1][0]:
                            sol += (float(A[0][f_2][0][f_1][0][f_0])/A[0][f_2][0][f_1][1])*fac1*fac2*f_(indeces[-1],f_0)

也许有人有想法。原则上,问题是迭代循环。 A 是一个以列表 [dict,int] 为节点的树结构。

【问题讨论】:

  • 您能提供一个完整的工作示例吗?目前尚不清楚fF_0F_1F_2Aindeces 应该是什么。
  • 递归通常适用于遍历任意(但相对较小的有限)深度的树结构;但我不知道该代码应该如何利用这种步行值(我也不想尝试解开它)。提供样本输入/输出不会有什么坏处。
  • 我相信你有两个错别字:1)你的float()的参数是...[f_0],这是一个子节点,不应该是一个数字吗?我猜最后缺少[1]。 2) 在最后一行的末尾,您使用f_(),应该是f() 还是其他函数?

标签: python loops tree cartesian-product


【解决方案1】:

在我将...[0] 替换为.children...[1] 替换为.node_val 之后,您的代码才开始对我有意义,如果您使用具有属性children 和@ 的正确Node 对象,就会发生这种情况987654327@,而不是列表:

sol = 0
for f_2 in F_2:
    if f_2 in A.children:
        fac1 = f(f_2,indeces[0])
        for f_1 in F_1:
            if f_1 in A.children[f_2].children:
                fac2 = f(f_1,indeces[1])
                for f_0 in F_0:
                    if f_0 in A.children[f_2].children[f_1].children:
                        sol +=(float(A.children[f_2].children[f_1].children[f_0])
                                    /A.children[f_2].children[f_1].node_val)
                                    *fac1*fac2*f_(indeces[-1],f_0)

您可能需要的是一个递归解决方案,类似于此(显然未经测试):

def recursive(F, indeces, node):
    assert len(F) == len(indeces)
    children = node[0]
    node_val = node[1]
    total = 0
    if len F > 1: # do recursive call
        for child_key in F[0]:
            if child_key in children:
                fac = f(child_key, indeces[0])
                total += fac * recursive(F[1:], indeces[1:], children[child_key])
    else: # only 1 item left
        for child_key in F[0]:
            if child_key in children:
                fac = f_(indeces[0], child_key)
                total += fac * (float(children[child_key]xx) / node_val)
    return total

F_X = [F_2, F_1, F_0]
ind = [i0, i1, i2]

result = recursive(F_X, ind, A)

xx 指的是我对您的问题的评论,看来您的float() 的参数是一个节点,而我本来希望是一个数字(node_val?)。

【讨论】:

  • 我试图获得一个交互式解决方案,但我猜深度如此之小,递归就可以了。实际上有4个选项:(1)实现所有情况,因为len(indeces)实际上在{2,3,4}中,(2)递归深度优先树遍历,(3)迭代深度优先树遍历,( 4) 一个函数,一旦确定 len(indeces) 的参数被设置,它就会给我写一个函数。老实说,我现在只是使用(1),但我仍然很想得到一个好的(2)或(3)。
【解决方案2】:

很快,这里有一个实施建议

class Node(object):
    def __init__(self, children={}, integer=0):
        self.dictionary = children
        self.integer    = integer

    @property
    def children(self):
        return self.dictionary


class Tree(object):
    def __init__(self):
        self.root = Node()

    def compute_your_thing(self, FF, indeces):
        """
        Assuming FF is a list of lists such that
            FF[0] = [f_00, f_01, f_02, ...]
            FF[1] = [f_10, f_11, f_12, ...]
            FF[2] = [f_20, f_21, f_22, ...]
            ...
            FF[len(indeces)]
        """
        # Generalize for any indeces length
        indeces_level = len(indeces)
        level = 0
        fac = []
        sol = 0

        def lookup(node, F):
            for f in F:
                if f in node.children:
                    new_node = node.children[f]
                    level += 1
                    if level == indeces_level:
                        # We have reached the depth defined by the length of indeces
                        sol += float(new_node.integer/node.integer) * times(fac) * foo(indeces[-1], f)
                    else :
                        fac.append(foo(f, indeces[level]))
                        return lookup(new_node, FF[indeces_level - level])

        # Use the nested loop to compute your thing recursively
        lookup(root, FF[indeces_level])
        return sol


# Utilities
def times(fac):
    result = 1
    for f in fac:
        result *= fac

    return result

def foo(ind, f):
    # I don't know what you do with this function
    pass

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-06-27
    • 2023-03-05
    • 1970-01-01
    • 2017-11-15
    • 2016-01-18
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多