将函数应用于列表中的一对元素

时间:2018-07-11 07:55:38

标签: python-3.x list loops iterator

我有一个这样的字符串列表:

sample = ["apple and apple and more apples", 
                  "apple, mangos and more apples", 
                  "pink is the new black or is it?",
                  "pink and pink with more red and black",
                  "apples are red and strawberry is marron/dark-pink"]

我想按顺序对这些字符串进行模糊匹配,例如fuzzy(sample[0], sample[1])fuzzy(sample[1], sample[2])fuzzy(sample[2], sample[3])fuzzy(sample[3], sample[4])

为此,我编写了这样的内容:

from fuzzywuzzy import fuzz

for i,j in zip(*[iter(sample)]*2):
    print(fuzz.QRatio(i, j))

但这只是产生fuzzy(sample[0], sample[1])fuzzy(sample[2], sample[3])的方式,如何获得其他两对字符串(即fuzzy(sample[1], sample[2]),{{1} }

1 个答案:

答案 0 :(得分:2)

改为使用sample[:-1]sample[1:]

print(list(map(fuzz.QRatio, sample[:-1], sample[1:])))