假設我有以下字串陣列:
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。
我怎么能那樣做?
uj5u.com熱心網友回復:
以下解決方案應該有效假設陣列如下
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)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/386028.html
