From vertical to horizontal output in python?

Viewed 448

I'm trying to create two lists of random dna sequences, dna1 and dna2. My code below is returning the random sequences of dna1 and dna2 in vertical order. I wonder how could I create a list for each dna result in horizontal format.

import numpy as np 
BASES = ('A','C','T','G')

def random_dna_sequence(length):
    return ''.join(np.random.choice(BASES,length)) #list() can be used 

with open('dna.txt', 'w+') as txtout:
    for _ in range(3):
        dna1 = random_dna_sequence(2)
        dna2 = random_dna_sequence(2)
        txtout.write(dna1)
        txtout.write(dna2)
        txtout.write("")
        print(dna1)
        print(dna2)

Output:

 CT
 AC
 AC

 AT
 AG
 CC

Expected outcome:

 dna1 = ['CT','AC','AC']
 dna2 = ['AT','AG','CC']
2 Answers

Do you have to use the file? If you need lists, then the code will be like this.

import numpy as np 
BASES = ('A','C','T','G')

def random_dna_sequence(length):
    return ''.join(np.random.choice(BASES,length)) #list() can be used 

dna1 = [random_dna_sequence(2) for _ in range(3)]
dna2 = [random_dna_sequence(2) for _ in range(3)]

print(dna1)
print(dna2)

Returned values:

['AG', 'CG', 'AG']
['TA', 'TC', 'AA']

Александр Рахмаев answered with a working solution.

Just want to highlight your issue here.

Already working: from array to pairs

Seems in your domain (Genetics) you are combining bases (single letters from a specific set of 4) to pairs (string of 2 letters).

Therefore you use numpy's function to randomize the combination: np.random.choice(BASES, 2). It will return an array, try print( np.random.choice(BASES, 2) ), looks similar to:

array(['A', 'T']) # 1 dimensional array with 2 random elements

Now you need to combine these 2 elements to a pair, joined with empty-string '', which means concatenated to a String. So print( ''.join(np.random.choice(BASES,2)) ) will print

AT

You can now add many of these random pairs to a list. Your code did so using a for-loop with 3 iterations:

for _ in range(3):  # for each of the 3 iterations do:
    print( random_dna_sequence(2) )  # print each random base-pair

In fact the output is 3 separate lines.

The other answer did same 3 iterations shorter, inside the brackets ([], list-comprehension): random_dna_sequence(2) for _ in range(3)

Issue: add each pair to a list (data structure)

Then you can print the finally filled list after the 3rd element has been added. Python's print-function will then automatically format the list as you expected, almost:

['AG', 'CG', 'AG']

List comprehension (pythonic)

What the other answer accomplished with list-comprehension inside the brackets:

# add 3 random pairs to a list, named variable: dna
dna = [random_dna_sequence(2) for _ in range(3)]
  • create a list dna and add all 3 elements from iteration inside brackets
  • the right-hand side of the assigning = is called list-comprehension

For loop (generic)

Same can be done by your for-loop. All you need to change is:

  • define dna as an empty list before the loop
  • add the pair to the list, inside your loop (for each of the 3 iterations): dna.add( pair )
  • print the list named dna after the loop (code formatted without indentation)

Difficulty for beginners

I found a similar question: New to learning python; why is the print for this for-loop different when I use a list comprehension? how do i make the loop be the same?. This might shed light on some difficulties.

Solution: adding pairs to a list

The file-writing is omitted!

dna1 = []  # define empty list
dna2 = []

for _ in range(3):
    dna1.append( random_dna_sequence(2) )  # add pair to list
    dna2.append( random_dna_sequence(2) )

print(dna1)  # print list containing 3 elements (3 pairs)
print(dna2)

Now, to add something in front, simply add inside the print-function - separated by comma: print('dna2 =', dna2) will output as expected

dna1 = ['AG', 'CG', 'AG']

For the file-output the question remains:

  • How do you expect the contents (here: the pairs) to be written?
    • A pair per line
    • All pairs in one line, maybe separated by comma or enclosed in quotes.
Related