I have this code:

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"


for i in range(len(protein)):
	print i+1,protein[i], pp[i], gor[i], aber[i]

I'm trying to compare these strings. However, the output is in a vertical format. How can I print it so that the format is vertical? This would make it much easier to compare!

Thanks!

Dani AI

Generated

The original loop prints one residue per iteration (index, protein[i], pp[i], ...), so output looks “vertical.” spotted that. For readable, side‑by‑side comparison the sequences should be printed in fixed‑width blocks so residues line up across rows. ’s chunking approach points in the right direction; the snippet below generalizes it to any number of labelled sequences and keeps blocks aligned even when lengths differ.

import textwrap
from itertools import zip_longest

def print_alignment(labels, seqs, width=70):
    blocks = [textwrap.wrap(s, width) for s in seqs]
    max_chunks = max(len(b) for b in blocks)
    for i in range(max_chunks):
        for label, blks in zip(labels, blocks):
            piece = blks[i] if i < len(blks) else ''
            print("{:>8}: {}".format(label, piece.ljust(width)))
        print()

For quick pairwise visual comparison (match line with | where characters are equal), a compact helper:

def print_pairwise_match(a_label, a_seq, b_label, b_seq, width=70):
    for i in range(0, max(len(a_seq), len(b_seq)), width):
        a = a_seq[i:i+width]
        b = b_seq[i:i+width]
        match = ''.join('|' if x == y else ' ' for x, y in zip_longest(a, b, fillvalue=' '))
        print("{:>8}: {}".format(a_label, a))
        print(" " * 10 + match)
        print("{:>8}: {}".format(b_label, b))
        print()

Notes and caveats: the examples assume Python 3 (print()); on Python 2 add from __future__ import print_function or adjust prints. zip_longest ensures shorter sequences are padded instead of truncating. For programmatic difference detection (rather than visual inspection) consider difflib or an LCS implementation as suggested. The block approach keeps residues aligned and makes spotting runs or disagreements much easier.

Recommended Answers

All 12 Replies

I'm not sure if this is what you want but:

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"


for i in range(len(protein)):
	print i+1,protein[i], pp[i], gor[i], aber[i], #<---- extra comma here

Enter in one extra comma after your final statement.

Hmm, actually I just re-read your question "How can I print it so that the format is vertical?," it's already being output in vertical format? Did you mean Horizontal? or do you want every single character coming out one line under the last one?

Thanks, but that's really messy and hard to look at. I was thinking something more like an alignment. You wouldn't happen to know how to output that would you?

Not off the top of my head, I'm a beginner myself.

Can you show an example of how you would like it to be output?

Like this (with lists protein and gor):

GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVL
cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccc
PHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL
hhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec

Like this

GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVL
cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccc
PHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL
hhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec

Here, this is better!

So, you want it to cut off at the 70th character?

No, haha! That was just the output from a protein prediction server.

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"


for i in range(len(protein)):
	print i+1,protein[i], pp[i], gor[i], aber[i] ,"\n"

This wil give you one check on each line.
If you want the result to be what you have pasted up there, then your code is entirely wrong.

Does anyone know how to achieve the output I want?

Sounds like you should compare the strings for matches in same place or Longest Common Subsequence anywhere instead of just printing them.

You can try this :

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"


print "".join([str(i / 10) for i in range(len(protein))])
print "".join([str(i) for i in range(len(protein))])
print protein
print pp
print gor
print aber

Or, with a way to choose the length :

protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL"

pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL"

gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec"

aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC"

length = 15

current = 0
for a in range(0, len(protein), 15):
    print "".join([str((i%100) / 10) for i in range(current, a)])
    print "".join([str(i % 10) for i in range(current, a)])
    print protein[current:a]
    print pp[current:a]
    print gor[current:a]
    print aber[current:a]
    print
    current = a
print "".join([str((i%100) / 10) for i in range(current, len(protein))])
print "".join([str(i % 10) for i in range(current, len(protein))])
print protein[current:len(protein)]
print pp[current:len(protein)]
print gor[current:len(protein)]
print aber[current:len(protein)]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.