2016-08-04 15 views
1

私は3つのものを持つヘッダ配列を持っています。私のプログラムは、並行しているか並行していないかを見て、すべてのヘッダの組み合わせを調べます。同じ行に文字列と変数を出力する

プログラムを実行すると、2つのヘッダーが並行していて、並行していないものを印刷します。だから、基本的には、代わりにそれは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) 

私はこの問題はライン4である知っていますと6。助けてください。次のようにこのコードでは、私は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]'はintではないと考えています – pwnsauce

+0

今あなたは良いです:)複数のマークを使って実際に '' {0 [{p [0]}]}と{1 [{p [1]}]}は並行して ".format(header、p = p)"ですが、今は私がそれを書くとひどく読めないように見えるので、そうしないでください。 –

関連する問題