您如何比較兩個列表並「回顧」?比較兩個列表並「回顧」
我比較兩個列表的內容是這樣的:
score = 0
for (x,y) in zip(seqA,seqB):
if x == y:
score = score +1
if x !=y :
score = score - 1
現在我想score + 3
如果以前對是比賽,所以基本上我將不得不「回頭看」一個迭代。
您如何比較兩個列表並「回顧」?比較兩個列表並「回顧」
我比較兩個列表的內容是這樣的:
score = 0
for (x,y) in zip(seqA,seqB):
if x == y:
score = score +1
if x !=y :
score = score - 1
現在我想score + 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
可能有更直接的辦法,但是明確也不錯。
添加想法引入一個變量,告訴我們下一次匹配哪個數量。多個連續兩場比賽
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
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
到其他人如何做到這一點,但我寧願使用的變量名類似像「正確」,而不是看到所有的地方「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
你的意思是+3如果以前的和當前的對是matc他是? – Hyperboreus
+3除了+1,還是+3代替+1? –