2011-07-20 16 views
0

2つのリストを比較して「戻って見る」方法はありますか?2つのリストを比較して「戻って見る」

私はこのような二つのリストの要素を比較しています:今

score = 0 
for (x,y) in zip(seqA,seqB): 

    if x == y: 
     score = score +1 

    if x !=y : 
     score = score - 1 

以前ペアが一致した場合、私はscore + 3をしたいと思いますので、基本的に、私は一回の反復を「振り返る」しなければなりません。

+0

以前のANDと現在のペアがmatc彼は? – Hyperboreus

+0

+3 +1に加えて、または+ 3の代わりに+3? –

答えて

3

最後に一致した結果を保存するだけです。

score = 0 
prev = 0 

for (x,y) in zip(seqA,seqB): 

    if x == y: 
     if prev == 1: 
      score = score +3 
     else: 
      score = score +1 
     prev = 1 

    if x !=y : 
     score = score - 1 
     prev = 0 
0

より直接的な方法があるかもしれませんが、明示的であっても悪くないです。
追加するアイデアは、次に一致するものを追加する量を示す変数を導入します。

連続した複数の一致のためのより洗練された報酬の規模は、いくつかの変更を導入することができ
score = 0 
matchPts = 1     // by default, we add 1 
for (x,y) in zip(seqA,seqB): 

    if x == y: 
     score = score + matchPts 
     matchPts = 3 

    if x !=y : 
     score = score - 1 
     matchPts = 1 

:他の人がこれを行っているのと同じように

score = 0 
consecutiveMatches = 0 
for (x,y) in zip(seqA,seqB): 

    if x == y: 
     consecutiveMatches += 1 
     reward = 1 
     if consecutiveMatches == 2: 
      reward = 3; 
     if consecutiveMatches > 2 : 
      reward = 5; 
     if consecutiveMatches > 5 : 
      reward = 100;  // jackpot ;-) 
     // etc. 
     score += reward 
    else: 
     score -= 1 
     consecutiveMatches = 0 
0
score = 0 
previousMatch == False 
for (x,y) in zip(seqa, seqb): 
    if x==y && previousMatch: 
     score += 3 
    elif x==y: 
     score += 1 
     previousMatch = True 
    else: 
     score -= 1 
     prviousMatch = False 
0

、私はむしろ、変数名を使用したいです"x == y"が全部表示されているのではなく "正しい"のように表示されます...

 
# Create a list of whether an answer was "correct". 
results = [x == y for (x,y) in zip(seqA, seqB)] 

score = 0 
last_correct = False 
for current_correct in results: 
    if current_correct and last_correct: 
     score += 3 
    elif current_correct: 
     score += 1 
    else: 
     score -= 1 

    last_correct = current_correct 

print score 
関連する問題