比较两张单子“回头看”

2024-10-01 19:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

你会如何比较两份清单并“回顾”呢?你知道吗

我正在比较两个列表的元素,如下所示:

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

    if x == y:
        score = score +1

    if x !=y :
        score = score - 1

现在我想score + 3如果上一个对是匹配的,那么基本上我必须“回顾”一次迭代。你知道吗


Tags: in元素列表forifzipscoreseqa
3条回答

可能有更直接的方法,但明确也不坏。
加法的思想引入了一个变量,它告诉我们下次匹配时要加多少。你知道吗

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
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
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

相关问题 更多 >

    热门问题