python - Creating combinations from a tuple -


i take data database. database come in form of tuple:

[('test1', 'test12', 'test13', 'test14'), ('test21', 'test22', 'test23', 'test24'),  ('test31', 'test32', 'test33', 'test34'),  ('test41', 'test42', 'test43', 'test44'),  ('test51', 'test52', 'test53', 'test54'),  ('test61', 'test62', 'test63', 'test64'),  ('test71', 'test72', 'test73', 'test74'),  ('test81', 'test82', 'test83', 'test84'),  ('test91', 'test92', 'test93', 'test94'),  ('test11', 'test12', 'test13', 'test14')] 

and that's want: make combinations of these input... output had combination of 4 parameters (such in example) and...

1) importantly, new combinations,the values in place, i.e. if in original combinations values index [1], means in new combination, should [1]...

2) there no duplicate combinations

as example:

i got tuple:

[('test91', 'test92', 'test93', 'test94'),  ('test11', 'test12', 'test13', 'test14')] 

and got new combinations:

[('test91', 'test12', 'test13', 'test14'),  ('test11', 'test92', 'test93', 'test94')] 

maybe it's possible using method of pairwise or else. help.

you need use product method builtin package itertools gives cartesian product of input iterables.

here code desire. but careful, because huge list produce ton of combinations, try not run out of memory.

from itertools import product   data = [('test91', 'test92', 'test93', 'test94'),  ('test11', 'test12', 'test13', 'test14')]  b = list(zip(*a))  # making list of n-th elements # b = [('test91', 'test11'),  <- first elements # ('test92', 'test12'),  <- second elements # ('test93', 'test13'),  <- third elements # ('test94', 'test14')] variations = product(*b) output = set(variations) - set(a)  # unique variations without input data #  output = {('test11', 'test12', 'test13', 'test94'), #  ('test11', 'test12', 'test93', 'test14'), #  ('test11', 'test12', 'test93', 'test94'), #  ('test11', 'test92', 'test13', 'test14'), #  ('test11', 'test92', 'test13', 'test94'), #  ('test11', 'test92', 'test93', 'test14'), #  ('test11', 'test92', 'test93', 'test94'), #  ('test91', 'test12', 'test13', 'test14'), #  ('test91', 'test12', 'test13', 'test94'), #  ('test91', 'test12', 'test93', 'test14'), #  ('test91', 'test12', 'test93', 'test94'), #  ('test91', 'test92', 'test13', 'test14'), #  ('test91', 'test92', 'test13', 'test94'), #  ('test91', 'test92', 'test93', 'test14')} 

if want output input data can

output = set(variations) 

if need list instead of set, do

output = list(output) 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -