简体   繁体   中英

Python - multiple combinations maths question

I'm trying to make a program that lists all the 64 codons/triplet base sequences of DNA... In more mathematical terms, there are 4 letters: A, T, G and C.

I want to list all possible outcomes where there are three letters of each and a letter can be used multiple times but I have no idea how!

I know there are 64 possibilities and I wrote them all down on paper but I want to write a program that generates all of them for me instead of me typing up all 64!

Currently, I am at this point but I have most surely overcomplicated it and I am stuck:

list = ['A','T','G','C']

list2 = []

y = 0

x = 1

z = 2

skip = False

back = False

for i in range(4):

 print(list[y],list[y],list[y])

  if i == 0:

   skip = True

  else:

    y=y+1

  for i in range(16):

    print(list[y],list[y],list[x])

    print(list[y],list[x], list[x])

    print(list[y],list[x], list[y])

    print(list[y],list[x], list[z])

   if i == 0:

      skip = True

  elif z == 3:

      back = True

      x = x+1

    elif back == True:

      z = z-1

      x = x-1

    else:

      x = x+1

      z = z+1

Any help would be much appreciated!!!!

You should really be using itertools.product for this.

from itertools import product

l = ['A','T','G','C']

combos = list(product(l,repeat=3 ))
# all 64 combinations

Since this produces an iterator, you don't need to wrap it in list() if you're just going to loop over it. (Also, don't name your list list — it clobbers the build-in).

If you want a list of strings you can join() them as John Coleman shows in a comment under your question.

list_of_strings = ["".join(c) for c in product(l,repeat=3) ]

Look for for pemuations with repetitions there tons of code available for Python . I would just use library , if you want to see how they implemented it look inside the library . These guys usually do it very efficiency

import itertools
x = [1, 2, 3, 4, 5, 6]
[p for p in itertools.product(x, repeat=2)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM