列出Python中RGB的所有排列
我试图列出所有可能的27种RGB 3字符排列。我的职能如下:列出Python中RGB的所有排列,python,rgb,Python,Rgb,我试图列出所有可能的27种RGB 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 rang
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']
结果如下:
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]的列表。我不明白为什么。这应该行得通:
from itertools import product
list(product('rgb', repeat=3))
这应该起作用:
from itertools import product
list(product('rgb', repeat=3))
问题是我们的
comb_list.append(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
。问题在于
comb_list.append(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
。一种解决方案:
import itertools
result = itertools.product("rgb",repeat = 3)
print(list(result))
一个解决方案:
import itertools
result = itertools.product("rgb",repeat = 3)
print(list(result))
您已将27个引用附加到
当前\u组合列表对象的comb\u list
。jp的解决方案很好。但为了防止您想保留代码,请更改附加到此的行:comb\u list.append(current\u composition.copy())
也可以。RGB没有27种排列。共有27个3字符组合。共有6种排列。您不断修改相同的当前组合
列表对象,并将对该对象的引用附加到组合列表
。所以当你打印comb_list
时,你只会看到27份current_composition
的当前内容。@Georgy当然,你的链接问题显示了如何获得笛卡尔积。但是它没有解决Hallvard的主要问题:comb\u list
为什么最终包含一堆重复的子列表?您已经向comb\u list
中的一个current\u组合添加了27个引用。jp的解决方案很好。但为了防止您想保留代码,请更改附加到此的行:comb\u list.append(current\u composition.copy())
也可以。RGB没有27种排列。共有27个3字符组合。共有6种排列。您不断修改相同的当前组合
列表对象,并将对该对象的引用附加到组合列表
。所以当你打印comb_list
时,你只会看到27份current_composition
的当前内容。@Georgy当然,你的链接问题显示了如何获得笛卡尔积。但是它没有解决Hallvard的主要问题:comb_list
为什么最终包含一堆重复的子列表?您不需要将list
应用于'rgb'
,您可以不应用。或者在Python 3中,您只需执行[*product('rgb',repeat=3)]
。在早期版本中,您可以执行list(product('rgb',repeat=3))
。您不需要将list
应用于'rgb'
,也可以不应用。或者在Python 3中,您只需执行[*product('rgb',repeat=3)]
。在早期版本中,您可以执行列表(product('rgb',repeat=3))
。啊,现在我明白了。谢谢你的解释!啊,现在我明白了。谢谢你的解释!