在python 3中打印格式

时间:2011-04-18 11:01:03

标签: python printing python-3.x

我正在尝试使用Python 3打印并遇到问题。我的代码中有一个for循环,如下所示:

seq = input("enter DNA sequence to search: ")
pat = re.compile('(.{10})(ATC.{3,6}CAG)')
li = []
output_lines = [] 
for mat in pat.finditer(seq):
    x = mat.end()
    li.append(mat.groups()+(seq[x:x+10],))
for u in li:
    z = u[1] 
    A = z.count('A')
    C = z.count('C') 
    G = z.count('G') 
    T = z.count('T')
    sumbases = [A,C,G,T]
    print(sumbases)

当我打印sumbases时,我得到了这个例子:

[1, 2, 3, 4]
[2, 0, 1, 4]

我正在尝试格式化输出:

[1, 2, 3, 4],[2, 0, 1, 4]

有人能告诉我这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

output_lines = []
for u in li:  
    z = u[1]  
    A = z.count('A')  
    C = z.count('C')   
    G = z.count('G')   
    T = z.count('T')
    sumbases = "A,C,G,T" # I suppose you format it here differently
    y = sumbases.replace("\n"," ")  # not sure why you need this

    # print(y) # don't print now, print later...
    output_lines.append(y)

print(','.join(output_lines))
编辑过的问题

编辑

seq = input("enter DNA sequence to search: ")
pat = re.compile('(.{10})(ATC.{3,6}CAG)')
output_lines = [] 
for mat in pat.finditer(seq):
    x = mat.end()
    z = (mat.groups()+(seq[x:x+10],)[1]
    output_lines.append(str([z.count(a) for a in 'ACGT')]))
print(','.join(output_lines))