Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,如何将列表中的一个句子与它后面出现的所有句子进行比较?_Python_Loops_For Loop - Fatal编程技术网

在Python中,如何将列表中的一个句子与它后面出现的所有句子进行比较?

在Python中,如何将列表中的一个句子与它后面出现的所有句子进行比较?,python,loops,for-loop,Python,Loops,For Loop,我有一个有1200个句子的列表。我想计算列表中一个句子的Jaccard系数,后面还有其他所有句子。 如sent1将与sent2、3进行比较,。。。然后用sent2和sent3,4,。。。 我已经有了一个函数,它接受2个集合并返回Jaccard系数。我只是想知道如何为上述场景编写python循环 list_question=[] #This List is later filled with sentences from a file def jaccard(a,b): # computes J

我有一个有1200个句子的列表。我想计算列表中一个句子的Jaccard系数,后面还有其他所有句子。 如sent1将与sent2、3进行比较,。。。然后用sent2和sent3,4,。。。 我已经有了一个函数,它接受2个集合并返回Jaccard系数。我只是想知道如何为上述场景编写python循环

list_question=[] #This List is later filled with sentences from a file

def jaccard(a,b): # computes Jaccard
    c=a.intersection(b)
    return float(len(c))/(len(a)+len(b)-len(c))

# ....Here i want to write the loop to compute the jaccard of sentences as explained in the question
我想根据Jaccard Coeff得分>0.5形成一组相似的句子

谢谢

您可以这样使用:

import itertools

def do_some_stuff(first, second):
    print('comapring', first, 'to', second)

sentences_list = ['fisrt', 'second', 'third', 'forth']
combinations = itertools.combinations(sentences_list, 2)
for first, second in combinations:
    do_some_stuff(first, second)
上面的代码段将为您提供以下输出:

comapring fisrt to second
comapring fisrt to third
comapring fisrt to forth
comapring second to third
comapring second to forth
comapring third to forth

与我们共享您的代码,没有人会从头开始,因为您使用了两个循环,内部循环由第一个循环的当前位置初始化。类似于
表示i…
表示范围内的j(i,…
编写程序的第一步是理解您试图解决的问题并描述解决方案。我发现关闭计算机并获得一支笔和一张纸通常会有所帮助。然后我用文字写下解决问题所需遵循的步骤。只有在我有了明确的解决方案后,我才能回到计算机上并启动t将其翻译成代码。请添加一些示例。这可能有助于您了解所需的输出以及您希望如何处理这些Jaccard系数。