如何在 Python 上打印多个字符串数组的每个可能排列?
How to print each possible permutation of several arrays of strings on Python?
假设我有以下字符串数组:
Background = {"Ocean"}
Body = {"Normal"}
Eyes = {"Big", "Small", "Monolid"}
Color = {"Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"}
Hands = {"None", "Robot", "Spider", "Bear"}
Extra = {"Empty", "Sand", "Dust", "Graffiti", "Aloe"}
我想打印一个列表,其中包含上面数组中提到的每个元素的所有可能排列,按照这些数组的设置顺序(即它开始检查 Background
,然后去检查Body
,然后是 Eyes
,然后是 Color
,然后是 Hands
,最后是 Extra
)。
第一个排列应该是:
1. Ocean, Normal, Big, Yellow, None, Empty
第二个排列应该是:
2. Ocean, Normal, Big, Yellow, None, Sand
等等...
可以假设项目None
与Empty
相同。
我该怎么做?
以下解决方案应该有效
假设数组如下
Backgrounds = ["Ocean"]
Bodys = ["Normal"]
Eyes = ["Big", "Small", "Monolid"]
Colors = ["Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"]
Hands = ["None", "Robot", "Spider", "Bear"]
Extra = ["Empty", "Sand", "Dust", "Graffiti", "Aloe"]
for background in Backgrounds:
for body in Bodys:
for eye in Eyes:
for colour in colours:
for hand in Hands:
for extra in extras:
print(background,body,eye,colour,hand,extra)
如果您的列表非常大,请不要使用上述解决方案,因为它的时间复杂度为 o(n^6)。
假设我有以下字符串数组:
Background = {"Ocean"}
Body = {"Normal"}
Eyes = {"Big", "Small", "Monolid"}
Color = {"Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"}
Hands = {"None", "Robot", "Spider", "Bear"}
Extra = {"Empty", "Sand", "Dust", "Graffiti", "Aloe"}
我想打印一个列表,其中包含上面数组中提到的每个元素的所有可能排列,按照这些数组的设置顺序(即它开始检查 Background
,然后去检查Body
,然后是 Eyes
,然后是 Color
,然后是 Hands
,最后是 Extra
)。
第一个排列应该是:
1. Ocean, Normal, Big, Yellow, None, Empty
第二个排列应该是:
2. Ocean, Normal, Big, Yellow, None, Sand
等等...
可以假设项目None
与Empty
相同。
我该怎么做?
以下解决方案应该有效 假设数组如下
Backgrounds = ["Ocean"]
Bodys = ["Normal"]
Eyes = ["Big", "Small", "Monolid"]
Colors = ["Yellow", "White", "Red Rose", "Turquoise", "Dark green", "Orange"]
Hands = ["None", "Robot", "Spider", "Bear"]
Extra = ["Empty", "Sand", "Dust", "Graffiti", "Aloe"]
for background in Backgrounds:
for body in Bodys:
for eye in Eyes:
for colour in colours:
for hand in Hands:
for extra in extras:
print(background,body,eye,colour,hand,extra)
如果您的列表非常大,请不要使用上述解决方案,因为它的时间复杂度为 o(n^6)。