pycharm - python - generate combinations (itertools)? -


can please give me hint put me on right path.

i need write python code come combinations solve following: need generate possible letter combinations. there 6 letters. letters can in each position e.g.

  • the first character can letters or t (2 possibilities)
  • the second character can a, t or c (3 possibilities)
  • the third character can g or t (2 possibilities)
  • the fourth character can a, t, c or g (4 possibilities)
  • the fifth character can c, g or t (3 possibilities)
  • the sixth character can b a, or t (2 possibilities)

i think should use python itertools examples have looked @ have fixed number of options available (in case letters) each position whereas letters available vary per position.

any appreciated.

you want use itertools.product task

you need create list of lists each possible value:

import itertools  l = [         ['a','t'],         ['a','t','c'],         ['g','t'],         ['a','t','c','g'],         ['c','g','t'],         ['a','t']     ]  out in itertools.product(*l):     print out; 

this outputs 288 items. small sample is:

('t', 't', 't', 'g', 'g', 't') ('t', 't', 't', 'g', 't', 'a') ('t', 't', 't', 'g', 't', 't') ('t', 'c', 'g', 'a', 'c', 'a') ('t', 'c', 'g', 'a', 'c', 't') ('t', 'c', 'g', 'a', 'g', 'a') ('t', 'c', 'g', 'a', 'g', 't') ('t', 'c', 'g', 'a', 't', 'a') ('t', 'c', 'g', 'a', 't', 't') ('t', 'c', 'g', 't', 'c', 'a') ('t', 'c', 'g', 't', 'c', 't') ('t', 'c', 'g', 't', 'g', 'a') ('t', 'c', 'g', 't', 'g', 't') ('t', 'c', 'g', 't', 't', 'a') ('t', 'c', 'g', 't', 't', 't') ('t', 'c', 'g', 'c', 'c', 'a') ('t', 'c', 'g', 'c', 'c', 't') ('t', 'c', 'g', 'c', 'g', 'a') 

as jonsharpe points out in the comment can have single list of valid strings (replacing l variable above this):

l = ['at','atc','gt','atcg','cgt','at'] 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -