嵌套的for循环元素列表比较

时间:2016-02-19 21:35:13

标签: python list for-loop iteration nested-loops

作为解决我所描述的挑战here的一种新方法,我将以下内容放在一起:

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

diffs =[
"""- It contains a Title II provision that changes the age at which workers
compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA).""",
"""+ It contains a Title II provision that changes the age at which workers 
compensation/public disability offset ends for disability beneficiaries from age 68 to full retirement age (FRA).""",
"""+ Here's a new paragraph I added for testing."""]

for s in diffs:
    others = [i for i in diffs if i != s]
    for j in others:
        if similar(s, j) > 0.7:
            print '"{}" and "{}" refer to the same sentence'.format(s, j)
            print
            diffs.remove(j)
        else:
            print '"{}" is a new sentence'.format(s)

这个想法是循环遍历字符串,并将每个字符串与其他字符串进行比较。如果给定的字符串被认为与另一个字符串相似,则删除另一个字符串,否则给定的字符串将被视为列表中的唯一字符串。

这是输出:

"- It contains a Title II provision that changes the age at which workers
compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA)." and "+ It contains a Title II provision that changes the age at which workers 
compensation/public disability offset ends for disability beneficiaries from age 68 to full retirement age (FRA)." refer to the same sentence


"- It contains a Title II provision that changes the age at which workers
compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA)." is a new sentence
"+ Here's a new paragraph I added for testing." is a new sentence

所以它正确地检测到前两个句子是相似的,而最后一个句子是唯一的。问题是它回过头来判断第一个句子是唯一的(它不是,并且它不应该回到这个句子)。

我的循环逻辑中的缺陷在哪里?这可以在没有嵌套for和删除元素的情况下实现吗?

3 个答案:

答案 0 :(得分:1)

from difflib import SequenceMatcher
from collections import defaultdict

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

diffs =[
"""- It contains a Title II provision that changes the age at which workers
compensation/public disability offset ends for disability beneficiaries from age 65 to full retirement age (FRA).""",
"""+ It contains a Title II provision that changes the age at which workers 
compensation/public disability offset ends for disability beneficiaries from age 68 to full retirement age (FRA).""",
"""+ Here's a new paragraph I added for testing."""]


sims = set()
simdict = defaultdict(list)
for i in range(len(diffs)):
    if i in sims:
        continue
    s = diffs[i]

    for j in range(i+1, len(diffs)):
        r = diffs[j]
        if similar(s, r) > 0.7:
            sims.add(j)
            simdict[i].append(j)


for k, v in simdict.iteritems():
    print diffs[k] + " is similar to:"
    print '\n'.join(diffs[e] for e in v)

答案 1 :(得分:0)

通过更改

,您可以确切地确定第一个句子是唯一的
print '"{}" is a new sentence'.format(s)

print '"{}" and "{}" are different sentences'.format(s,j)

这可以帮助您查看您的循环失败的确切位置。

答案 2 :(得分:0)

由于修改后的字符串将始终背靠背显示(一个前面带有' - ',另一个'+'和' - ',所以可以做到(我相信它会适用于所有情况)。

当列表具有奇数个元素时,最后一个必须是新句子。

def extract_modified_and_new(diffs):
    for z1, z2 in zip(diffs[::2], diffs[1::2]):
        if similar(z1, z2) > 0.7:
            print z1, 'is similar to', z2
            print
        else:
            print z1, ' and ', z2, 'are new'
            print
    if len(diffs) % 2 != 0:
            print diffs[-1], ' is new'