Abstract: make combinations with a given data pool
The result:
cartesian product between abcd and 1234:
16 [('a', '1'), ('a', '2'), ('a', '3'), ('a', '4'), ('b', '1'), ('b', '2'), ('b', '3'), ('b', '4'), ('c', '1'), ('c', '2'), ('c', '3'), ('c', '4'), ('d', '1'), ('d', '2'), ('d', '3'), ('d', '4')]
['0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111', '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111']
combinations:
['123', '124', '125', '126', '134', '135', '136', '145', '146', '156', '234', '235', '236', '245', '246', '256', '345', '346', '356', '456']
combinations with replacement
['111', '112', '113', '114', '115', '116', '122', '123', '124', '125', '126', '133', '134', '135', '136', '144', '145', '146', '155', '156', '166', '222', '223', '224', '225', '226', '233', '234', '235', '236', '244', '245', '246', '255', '256', '266', '333', '334', '335', '336', '344', '345', '346', '355', '356', '366', '444', '445', '446', '455', '456', '466', '555', '556', '566', '666']
permuations:
['1234', '1243', '1324', '1342', '1423', '1432', '2134', '2143', '2314', '2341', '2413', '2431', '3124', '3142', '3214', '3241', '3412', '3421', '4123', '4132', '4213', '4231', '4312', '4321']
ok
The script:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 17:17:06 2015
@author: yuan
"""
import itertools
if __name__=="__main__":
#cartesian product
a='abcd'
b='1234'
ip=itertools.product(a,b)
dp=[i for i in ip]
print 'cartesian product between %s and %s:' % (a,b)
print len(dp),dp
#
com=[]
for ip in itertools.product([0,1], repeat=4):
c=[str(i) for i in ip]
com.append(''.join(c))
print com
########################itertools.combinations()
print 'combinations:'
com=[]
for ip in itertools.combinations([1,2,3,4,5,6], 3):
c=[str(i) for i in ip]
com.append(''.join(c))
print com
print 'combinations with replacement'
com=[]
for ip in itertools.combinations_with_replacement([1,2,3,4,5,6], 3):
c=[str(i) for i in ip]
com.append(''.join(c))
print com
########################itertools.permutations()
print 'permuations:'
com=[]
for ip in itertools.permutations([1,2,3,4]):
c=[str(i) for i in ip]
com.append(''.join(c))
print com
print 'ok'
No comments:
Post a Comment