[英]How to print each possible permutation of several arrays of strings on Python?
假设我有以下 arrays 字符串:
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"}
我想打印一个列表,其中包含上面 arrays 中提到的每个元素的所有可能排列,按照设置这些 arrays 的顺序(即它开始检查Background
,然后检查Body
,然后是Eyes
,然后是Color
,然后Hands
,并在Extra
上完成)。
第一个排列应该是:
1. Ocean, Normal, Big, Yellow, None, Empty
第二个排列应该是:
2. Ocean, Normal, Big, Yellow, None, Sand
等等...
可以假设项目None
与Empty
相同。
我怎么能那样做?
以下解决方案应该有效假设 arrays 如下
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)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.