所有具有多个循环的组合

时间:2014-05-02 11:35:23

标签: python loops iteration combinations

我想基于用户决定使用的数字创建组合求解器,并获得第一个循环中每个项目的其余循环的所有组合,例如:

intNumberOfLoops = 3
lstLoopOne = ['loop01item01', 'loop01item02', 'loop01item03', 'loop01item04', 'loop0item05']

#Since we will be getting all combinations in relation to lstLoopOne, we will always need at #least one loop and thus we subtract it from intNumberOfLoops

if intNumberOfLoops < 2:
   print("Gotta put in more than 2 loops")
   exit()
for i in range(intNumberOfLoops-1):
   #create loops and same number of items as lstLoopOne dynamically for example:

   lstLoopTwo = ['loop02item01', 'loop02item02', 'loop02item03', 'loop0item04', 'loop0item05']
   lstLoopThree = ['loop03item01', 'loop03item02', 'loop03item03', 'loop03item04', 'loop03item05']

   #since we dont know how many loops the user wants to use at design-time, maybe we would have to use a list for lists??? But how would we get the list names which are based on the loop number and put it into a list of lists?

   #then run itertools.combinations on every item in the list of lists for example:

   for w in lstOfLists:
      #dynamicvariable = w.itertools.combinations
   #for every dynamic variable that was created by previod for:
      print(#all loop combinations in this case it would be for three loops:
      #loop01item01, loop02item01, loop03item01 <-iteration 01
      #loop01item01, loop02item01, loop03item02 <-iteration 02     
      #loop01item01, loop02item01, loop03item03 <-iteration 03
      #loop01item01, loop02item01, loop03item04 <-iteration 04
      #loop01item01, loop02item01, loop03item05 <-iteration 05
      ...
      #loop01item01, loop02item02, loop03item01 <-iteration n
      #loop01item01, loop02item02, loop03item02 <-iteration n+1
      ...
      #loop01item02, loop02item01, loop03item01 <-iteration t
      #loop01item02, loop02item01, loop03item02 <-iteration t+1
      #loop01item02, loop02item01, loop03item03 <-iteration t+2
      ...
      #loop01item02, loop02item02, loop03item01 <-iteration s
      #loop01item02, loop02item02, loop03item02 <-iteration s+1
      ...

1 个答案:

答案 0 :(得分:2)

使用itertools.product

import itertools
lists = [
    ['loop01item01', 'loop01item02', 'loop01item03', 'loop01item04', 'loop0item05'],
    ['loop02item01', 'loop02item02', 'loop02item03', 'loop0item04', 'loop0item05'],
    ['loop03item01', 'loop03item02', 'loop03item03', 'loop03item04', 'loop03item05']
]

loops = int(raw_input("Enter the number of nested loops you want: "))
for elements in itertools.product(*lists[:loops]):
    print elements

结果:

Enter the number of nested loops you want: 3
('loop01item01', 'loop02item01', 'loop03item01')
('loop01item01', 'loop02item01', 'loop03item02')
('loop01item01', 'loop02item01', 'loop03item03')
('loop01item01', 'loop02item01', 'loop03item04')
('loop01item01', 'loop02item01', 'loop03item05')
...
('loop01item01', 'loop02item02', 'loop03item01')
('loop01item01', 'loop02item02', 'loop03item02')
...
('loop01item02', 'loop02item01', 'loop03item01')
('loop01item02', 'loop02item01', 'loop03item02')
('loop01item02', 'loop02item01', 'loop03item03')
...
('loop01item02', 'loop02item02', 'loop03item01')
('loop01item02', 'loop02item02', 'loop03item02')

如果您不希望任何元素共享索引,请在列表索引上使用permutations

import itertools
lists = [
    ['loop01item01', 'loop01item02', 'loop01item03', 'loop01item04', 'loop0item05'],
    ['loop02item01', 'loop02item02', 'loop02item03', 'loop0item04', 'loop0item05'],
    ['loop03item01', 'loop03item02', 'loop03item03', 'loop03item04', 'loop03item05']
]

loops = int(raw_input("Enter the number of nested loops you want: "))
list_size = len(lists[0])
for indices in itertools.permutations(range(5), loops):
    elements = [lists[i][idx] for i, idx in enumerate(indices)]
    print elements