2016-08-04 42 views
1

我有一個包含三件事情的標題數組。我的程序遍歷所有標題組合,並查看它們是併發還是不併發。在同一行上打印字符串和變量

當我運行程序時,我希望它打印出哪兩個頭是併發的,哪些不是併發的。當它打印的,而不是將它打印sequences are concurrent/sequences are not concurrent所以基本上,我希望它說header a is concurrent to header bheader b is not concurrent to header c

這是我的計劃,因爲它代表:

c=combinations(header,2) 
for p in combinations(sequence,2): 
    if p[0][start:stop]==p[1][start:stop]: 
     print header[p[0],p[1]], "are concurrent" 
    else: 
     print header[p[0],p[1]], "are not concurrent" 
print list(c) 

我知道問題出在四線制和六個。請幫忙。有了這個代碼,我得到TypeError: list indices must be integers, not tuple.

有人問我的頭和序列的例子... 我的頭如下: ( '> DQB1', '> OMIXON', '> GENDX')

我的序列如下:在的Python ( 'GACTAAAAAGCTA', 'GACTAAAAAGCTA', 'GAAAACTGGGGGA')

+1

是不是隻是'頭[P [0],標題[P [1]],...'? –

+0

'print header [p [0]],「與...並行」,header [p [1]]'?如果你提供了什麼'header'和'sequence'的例子,這將有所幫助 – BusyAnt

+0

這個錯誤意味着'p [0],p [1]'不是一個整數,'header []'只接受一個int (如果標題是一個列表) – pwnsauce

回答

2

希望將兩個清單合併爲一個:

for (h1, s1), (h2, s2) in combinations(zip(header, sequence), 2): 
    if s1[start:stop] == s2[start:stop]: 
     print h1, h2, "are concurrent" 
    else: 
     print h1, h2, "are not concurrent" 

或減少重複代碼:

for (h1, s1), (h2, s2) in combinations(zip(header, sequence), 2): 
    concurrent = s1[start:stop] == s2[start:stop] 
    print "{} and {} are{} concurrent".format(h1, h2, "" if concurrent else " not") 
+0

或者只是'print h1,h2'是'如果s1 [start:stop] == s2 [start:stop] else'不是','concurrent''。 – arekolek

0

的最佳方式的格式字符串是這樣的:

"{} and {} are concurrent".format(header[p[0]],header[p[1]]) 

也可以使用多個佔位符{}

+0

不,它不。 –

+0

你怎麼知道?我認爲'P [X]'不是一個整數 – pwnsauce

+0

現在你很好:)雖然有多個標記,你可以實際做'{0 [{p [0]}]和{1 [{p [1]}]}是併發的.format(header,p = p)'雖然現在我寫出來了,但看起來非常不可讀,所以不要這麼做。 –

相關問題