【问题标题】:Listing all permutations of RGB in Python在 Python 中列出 RGB 的所有排列
【发布时间】:2018-07-19 02:32:06
【问题描述】:

我正在尝试列出 RGB 的所有可能的 27 个 3 字符排列。我的功能如下:

def make_big_RGB():
    rgb = ('r', 'g', 'b')
    comb_list = []
    current_combination = list(rgb) #start with combination (r, g, b)
    for x in range(3):
        current_combination[0] = rgb[x]
        for y in range(3):
            current_combination[1] = rgb[y]
            for z in range(3):
                current_combination[2] = rgb[z]
                comb_list.append(current_combination)
                print('Added' + str(current_combination))
    for _ in comb_list:
        print(_)
make_big_RGB()

结果如下:

Added['r', 'r', 'r']
Added['r', 'r', 'g']
Added['r', 'r', 'b']
Added['r', 'g', 'r']
Added['r', 'g', 'g']
Added['r', 'g', 'b']
Added['r', 'b', 'r']
Added['r', 'b', 'g']
Added['r', 'b', 'b']
Added['g', 'r', 'r']
Added['g', 'r', 'g']
Added['g', 'r', 'b']
Added['g', 'g', 'r']
Added['g', 'g', 'g']
Added['g', 'g', 'b']
Added['g', 'b', 'r']
Added['g', 'b', 'g']
Added['g', 'b', 'b']
Added['b', 'r', 'r']
Added['b', 'r', 'g']
Added['b', 'r', 'b']
Added['b', 'g', 'r']
Added['b', 'g', 'g']
Added['b', 'g', 'b']
Added['b', 'b', 'r']
Added['b', 'b', 'g']
Added['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']
['b', 'b', 'b']

最后一个 for 循环打印出想要的结果,但是当我随后尝试打印整个列表时,结果不知何故是一个包含 27 项 [b, b, b] 的列表。我不明白为什么。

【问题讨论】:

  • 您已将 27 个对 current_combination 列表对象的引用附加到 comb_list
  • jp 的解决方案很好。但以防万一你想保留你的代码,改变你附加到这个的行:comb_list.append(current_combination.copy()) 应该也可以。
  • RGB 没有 27 种排列。共有 27 个 3 字符组合。有 6 种排列。
  • 您不断修改相同的current_combination 列表对象并将对该one 对象的引用附加到comb_list。因此,当您打印 comb_list 时,您将看到的只是 current_combination 的当前内容的 27 个副本。
  • @Georgy 当然,您的链接问题显示了如何获得笛卡尔积。但这并没有解决 Hallvard 的主要问题:为什么 comb_list 最终会包含一堆重复的子列表?

标签: python rgb


【解决方案1】:

这应该可行:

from itertools import product

list(product('rgb', repeat=3))

【讨论】:

  • 你不需要将list申请到'rgb',你可以不用。
  • 或者在 Python 3 中,你可以直接使用 [*product('rgb', repeat=3)]。在早期版本中,您可以使用list(product('rgb', repeat=3))
【解决方案2】:

问题在于

comb_list.append(current_combination)

您将相同列表一次又一次地添加到结果列表中。因此更改该列表中的值,例如current_combination[0] = rgb[x] 也会改变所有“其他”列表中的值。

您可以通过在插入之前从该列表创建一个新列表来解决此问题:

comb_list.append(list(current_combination))

或者在将其添加到结果时直接从三个元素创建该列表:

for x in rgb:
    for y in rgb:
        for z in rgb:
            comb_list.append([x, y, z])

或者只是对整个事情使用列表理解:

comb_list = [[x, y, z] for x in rgb for y in rgb for z in rgb]

或使用itertools.product,如另一个答案中所述。

【讨论】:

  • 啊,现在我明白了。谢谢你的解释!
【解决方案3】:

一个解决方案:

import itertools

result = itertools.product("rgb",repeat = 3)    
print(list(result))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多