比较两个字符串中的每个字符并递增:Python

2024-05-03 04:46:45 发布

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

for x in predRslt:
    for y in actRslt:
        if x == y and x =='1':
            trueP += 1
        elif x == y and x =='0':
            trueN += 1
        elif x != y and x == '1':
            falseN += 1      
        elif x != y and x == '0':
            falseP += 1

    charNum += 1
totalActN = trueN + falseP
totalActP = falseN + trueP
totalPredN = trueN + falseN
totalPredP = trueP + falseP

print falseP

cmp_rslt('01101001','1100100101')

实际产出:25

预期产出:2

我试着从一开始就依次检查每个字符串并比较结果。然后增加相应的TrueN、TrueP、FalseN或FalseP

出于某种原因,我一直得到25的输出,而我应该得到比这个小得多的输出,因为我只需要进行10次比较


Tags: andinforifelifcharnumtotalactntotalpredn
1条回答
网友
1楼 · 发布于 2024-05-03 04:46:45

您应该使用第一个字符串的索引进行迭代,如下所示:

for idx, x in enumerate(predRslt);
    y = actRslt[idx]

    if x == y and x =='1':         
        trueP += 1
    elif x == y and x =='0':
        trueN += 1
    elif x != y and x == '1':
        falseN += 1      
    elif x != y and x == '0':
        falseP += 1

相关问题 更多 >