source: git/modules/python/cart.py @ 6d4c80

spielwiese
Last change on this file since 6d4c80 was 6d4c80, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken:cart.py git-svn-id: file:///usr/local/Singular/svn/trunk@8670 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 1.1 KB
Line 
1import itertools
2def xcombine(*seqin):
3    '''returns a generator which returns combinations of argument sequences
4for example xcombine((1,2),(3,4)) returns a generator; calling the next()
5method on the generator will return [1,3], [1,4], [2,3], [2,4] and
6StopIteration exception.  This will not create the whole list of
7combinations in memory at once.'''
8    def rloop(seqin,comb):
9        '''recursive looping function'''
10        if seqin:                   # any more sequences to process?
11            for item in seqin[0]:
12                newcomb=comb+[item]     # add next item to current combination
13                # call rloop w/ remaining seqs, newcomb
14                for item in rloop(seqin[1:],newcomb):   
15                    yield item          # seqs and newcomb
16        else:                           # processing last sequence
17            yield comb                  # comb finished, add to list
18    return rloop(seqin,[])
19
20def cartn(sequence, n):
21  tocombine=list(itertools.repeat(sequence,n))
22  return itertools.imap(tuple,xcombine(*tocombine))
23 
24if __name__=='__main__':
25  for i in cartn([1,2,3],4):
26    print i
Note: See TracBrowser for help on using the repository browser.