【问题标题】:How to cycle through permutations of a list of 88 elements?如何循环遍历 88 个元素列表的排列?
【发布时间】:2020-01-25 22:05:15
【问题描述】:

我正在尝试为 88 个元素列表的每个排列一个接一个地分配一个变量。这意味着该变量将被重新分配一个新的排列 88!次。我尝试过制作 88 个嵌套的 for 循环,但显然你可以嵌套多少个 for 循环是有限制的。我正在使用 Python。

elements = [[211,76], [235,84], [240,88], [300,79], [230,100], [203,110], [202,95], [203,98], [214,97], [248,137], [249,111], [282,120], [263,144], [245,167], [192,172], [124,161], [322,146], [338,142], [355,146], [322,90], [343,105], [363,105], [368,116], [349,83], [348,67], [348,56], [390,75], [327,69], [443,73], [363,96], [396,104], [467,87], [499,87], [197,201], [199,200], [229,201], [227,219], [243,235], [403,166], [370,189], [399,212], [408,227], [214,292], [297,236], [352,276], [430,304], [437,348], [457,347], [430,277], [459,317], [709,153], [772,113], [828,103], [758,308], [767,320], [771,325], [803,330], [778,346], [795,348], [808,341], [834,341], [831,359], [762,391], [764,402], [817,404], [844,426], [827,440], [862,467], [729,433], [732,449], [747,477], [767,480], [657,454], [647,468], [649,467], [626,480], [670,499], [645,524], [652,547], [649,562], [688,566], [580,645], [580,645], [387,632], [419,625], [417,617], [419,614], [575,419]]
for x1 in elements:
    tempelements1 = elements.copy()
    tempelements1.remove(x1)
    for x2 in tempelements1:
        tempelements2 = tempelements1.copy()
        tempelements2.remove(x2)
        for x3 in tempelements2:
            tempelements3 = tempelements2.copy()
            tempelements3.remove(x3)
            for x4 in tempelements3:
                tempelements4 = tempelements3.copy()
                tempelements4.remove(x4)
                for x5 in tempelements4:
                    tempelements5 = tempelements4.copy()
                    tempelements5.remove(x5)
                    #...
                        for x88 in tempelements87:
                            for y1 in [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23, x24, x25, x26, x27, x28, x29, x30, x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44, x45, x46, x47, x48, x49, x50, x51, x52, x53, x54, x55, x56, x57, x58, x59, x60, x61, x62, x63, x64, x65, x66, x67, x68, x69, x70, x71, x72, x73, x74, x75, x76, x77, x78, x79, x80, x81, x82, x83, x84, x85, x86, x87, x88]:

【问题讨论】:

  • @jonrsharpe 你想让我发布 88 个嵌套 for 循环的代码吗?这些循环遍历 P(88,88) 排列?
  • 88 个嵌套循环无法工作。但是 99 个嵌套循环可能 ;) 严重: 1) 请准确解释您要做什么。目前尚不清楚您的“目标”是什么,更不用说您如何尝试解决它了。 2)minimal reproducible example 会有所帮助。即使是伪代码也可能有所帮助。 3) 问:您使用什么语言?
  • 好的:知道了。您只是想以最不高效的方式计算列表上的迭代 ;) 另一种方法可能是使用itertools。看这里:geeksforgeeks.org/…。而且 KamikazeJones 是正确的:n = 88! 是一个难以处理的大列表;)
  • PS:Python 对您可以在程序中编码的“#/nested 循环”没有限制。但出于实际目的,在考虑替代方法之前,我绝不会嵌套任何深度超过 3 或 4(如果有的话!)的循环。编写一个新函数,创建一个新类……或者考虑使用recursion。兼顾“性能”和“可读性”。
  • 当你说“我试图找到这张图片中的所有坐标组,其中一组坐标之间的距离小于 215.05813167606567”时,这是否意味着真正的问题是聚类问题其中元素的子列表是 Lat/Long 并且您正在尝试找到最大距离为 215.05 的集群的分区...?

标签: python for-loop nested permutation


【解决方案1】:

这不会在您的有生之年终止。 88! = 1.854826e+134 这就像计算整个宇宙中的所有原子。 您必须针对您的问题尝试不同的解决策略。

这里是递归置换列表的代码,它避免了显式嵌套循环

def permuteIt(resultList,elementList):
    if len(elementList)==0:
        yield resultList
    else:
        for i in range(0,len(elementList)):
            resultList.append(elementList.pop(i))
            for l in permuteIt(resultList, elementList):
                yield resultList
            elementList.insert(i,resultList.pop())

for x in permuteIt([],[1,4,7,11]):
    print "result: ",x

【讨论】:

    【解决方案2】:

    正如另一位用户所说,您可能需要一种不同的算法:88 个元素的排列数量非常多。也就是说,如果您想要排列,请查看标准库的 itertools 模块:

    from itertools import permutations
    
    arr = [2,3,5]
    for x in permutations(arr):
        print(x)
    

    打印:

    (2, 3, 5)
    (2, 5, 3)
    (3, 2, 5)
    (3, 5, 2)
    (5, 2, 3)
    (5, 3, 2)
    

    【讨论】:

    • 是的,但是要遍历排列,代码必须制作排列列表,并且没有足够的内存来保存 88 个列表!排列。
    • @nacewv 排列是惰性计算的,并且不会同时存储在内存中。即使像for x in itertools.permutations(range(1_000)): print(x) 这样的东西也不会在内存使用上爆炸。这是因为该函数返回的是一个迭代器,而不是一个列表。
    • 好的,但是否可以遍历 88!排列??如果是这样,那么这对我有用
    • 没有。 88!纳秒仍然是宇宙年龄的 10^100 倍以上。
    猜你喜欢
    • 2018-09-05
    • 2018-07-11
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2017-02-08
    相关资源
    最近更新 更多