排列:产生循环符号

2024-06-15 06:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

请考虑以下问题:我有一定的排列西格玛:

sigma = [4,1,6,2,3,5]

期望的结果是产生如下循环符号:

^{pr2}$

我的尝试按照下面的代码进行,但是似乎我只到达第一个周期,但似乎无法重新进入第二个周期:

my_row_cycle函数背后的想法是取某个排列sigma,设置一种称为marker(标记==0时crcuit闭合)的断路器,并在排列上迭代,直到我完成一个循环,一旦循环完成,我将其存储到一个列表中。在

然后,我通过再次迭代sigma来验证是否还有其他循环要从置换中提取,直到我在sigma中找到一个不在之前提取的循环中的数字。如果找到这样的号码,我会重新启动进程。如果没有,我会使断路器跳闸,marker==1结束整个过程并输出sigma置换的循环符号。在

但对我来说,这仍然是一个乌托邦。:)

def my_row_cycle(sigma):
    aux_sigma = list(sigma)
    init_ref = aux_sigma.index(aux_sigma[0]) +1       #First antecedent of aux_sigma
    init_image = aux_sigma[init_ref-1]                #Image of the antecedent
    jumper_image = init_image
    row_cycle = []
    row_cycle.append(init_image)
    my_cycles_perm = []

    marker = 0

    while marker == 0:                                 #Circuit breaker
        while jumper_image != init_ref:                #establishes if cycle complete
            for x in aux_sigma:                     #iterates sigma while cycle incomplete
                jumper_ref = aux_sigma.index(x) + 1
                if jumper_ref == jumper_image:         #Condition to append to cycle
                    row_cycle.append(x)
                    jumper_image = x                   #Changing the while loop condition
        my_cycles_perm.append(row_cycle)

        for a in aux_sigma:
            for a in my_cycles_perm:
                cycle = a
                for a in cycle:                  #looking for match in aux_sigma and cycle
                    if a not in cycle:
                        row_cycle = []
                        init_image = a
                        init_ref = aux_sigma.index(init_image) + 1
                        marker = 0
                        break

                    else:
                        marker = 1

return init_ref, init_image, jumper_image, jumper_ref, row_cycle, marker, my_cycles_perm

评估后:

(1, 4, 1, 6, [4, 2, 1], 1, [[4, 2, 1]])

我似乎不明白为什么我的记号笔到了值“1”,而我的循环符号却不完整。 如果您有任何建议和/或更正,我将提前感谢您。在


Tags: inimagerefforinitmysigmamarker
2条回答

我相信这个功能可以实现您的愿望:

def to_cycles(perm):
    pi = {i+1: perm[i] for i in range(len(perm))}
    cycles = []

    while pi:
        elem0 = next(iter(pi)) # arbitrary starting element
        this_elem = pi[elem0]
        next_item = pi[this_elem]

        cycle = []
        while True:
            cycle.append(this_elem)
            del pi[this_elem]
            this_elem = next_item
            if next_item in pi:
                next_item = pi[next_item]
            else:
                break

        cycles.append(cycle)

    return cycles

print(to_cycles([]))
# []

print(to_cycles([1]))
# [[1]]

print(to_cycles([4,1,6,2,3,5]))
# [[4, 2, 1], [6, 5, 3]]

可能是因为您意外地在嵌套循环中多次使用变量a

for a in aux_sigma :
    for a in my_cycles_perm :
        cycle = a
        for a in cycle :        # <<  a iterates ovr cycles, so
            if a not in cycle : # <<  a in cycle is alsways true
                # [...]
                marker = 1
            else :
                marker = 0

为所有不同的迭代指定单独的变量名。在

相关问题 更多 >