for-Python循环中出现意外输出

for-Python循环中出现意外输出,python,string,list,Python,String,List,我有以下清单: t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']] 我想根据jaccard距离对列表重新排序。如果我重新排序t,预期输出应为: [['universitario de deportes'],['universitario de'],['lancaster'],['juan aurich'],['juan'],['muni']]

我有以下清单:

t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']]
我想根据jaccard距离对列表重新排序。如果我重新排序
t
,预期输出应为:

[['universitario de deportes'],['universitario de'],['lancaster'],['juan aurich'],['juan'],['muni']]
jackard distance的代码工作正常,但其余代码未给出预期输出。代码如下:

def jack(a,b):
    x=a.split()
    y=b.split()
    k=float(len(set(x)&set(y)))/float(len((set(x) | set(y))))
    return k
t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']]

import copy as cp


b=cp.deepcopy(t)

c=[]

while (len(b)>0):
    c.append(b[0][0])
    d=b[0][0]
    del b[0]
    for m in range (0 , len(b)+1):
        if m > len(b):
            break
            if jack(d,b[m][0])>0.3:
                c.append(b[m][0])
                del b[m]
t=['universitario de deportes','lancaster','lancaste','juan aurich','lancaster','juan','universitario','juan franco']

import copy as cp


b=cp.deepcopy(t)

c=[]

while (len(b)>0):
    c.append(b[0])
    e=b[0]
    del b[0]
    for val in b:
        if jack(e,val)>0.3:
            c.append(val)
            b.remove(val)

print c
['universitario de deportes', 'universitario', 'lancaster', 'lancaster', 'lancaste', 'juan aurich', 'juan', 'juan franco'
不幸的是,意外输出是相同的列表:

print c
['universitario de deportes', 'lancaster', 'universitario de', 'juan aurich', 'muni', 'juan']
编辑:


我试图更正我的代码,但它也不起作用,但我离预期的输出更近了一点:

t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']]

import copy as cp


b=cp.deepcopy(t)

c=[]

while (len(b)>0):
    c.append(b[0][0])
    d=b[0][0]
    del b[0]
    for m in range(0,len(b)-1):
        if jack(d,b[m][0])>0.3:
            c.append(b[m][0])
            del b[m]
“关闭”输出为:

['universitario de deportes', 'universitario de', 'lancaster', 'juan aurich', 'muni', 'juan']
第二次编辑:

最后,我提出了一个计算速度相当快的解决方案。目前,我将使用该代码订购6万个名称。代码如下:

def jack(a,b):
    x=a.split()
    y=b.split()
    k=float(len(set(x)&set(y)))/float(len((set(x) | set(y))))
    return k
t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']]

import copy as cp


b=cp.deepcopy(t)

c=[]

while (len(b)>0):
    c.append(b[0][0])
    d=b[0][0]
    del b[0]
    for m in range (0 , len(b)+1):
        if m > len(b):
            break
            if jack(d,b[m][0])>0.3:
                c.append(b[m][0])
                del b[m]
t=['universitario de deportes','lancaster','lancaste','juan aurich','lancaster','juan','universitario','juan franco']

import copy as cp


b=cp.deepcopy(t)

c=[]

while (len(b)>0):
    c.append(b[0])
    e=b[0]
    del b[0]
    for val in b:
        if jack(e,val)>0.3:
            c.append(val)
            b.remove(val)

print c
['universitario de deportes', 'universitario', 'lancaster', 'lancaster', 'lancaste', 'juan aurich', 'juan', 'juan franco'

首先,我不知道为什么你会把所有的东西都放在单件清单上,所以我建议你先把它放平:

t = [l[0] for l in t]
这将消除所有额外的零索引,这意味着您只需要浅拷贝(因为字符串是不可变的)

其次,代码的最后三行永远不会运行:

if m > len(b):
    break # nothing after this will happen
    if jack(d,b[m][0])>0.3:
       c.append(b[m][0])
       del b[m]
我想你想要的是:

out = [] # this will be the sorted list
for index, val1 in enumerate(t): # work through each item in the original list
    if val1 not in out: # if we haven't already put this item in the new list
        out.append(val1) # put this item in the new list
    for val2 in t[index+1:]: # search the rest of the list
        if val2 not in out: # if we haven't already put this item in the new list
            jack(val1, val2) > 0.3: # and the new item is close to the current item
                out.append(val2) # add the new item too
这给了我

out == ['universitario de deportes', 'universitario de', 
      'lancaster', 'juan aurich', 'juan', 'muni']

我通常建议使用比
a
b
c
等更好的变量名。

为什么
t
包含单个项目列表?对您的值运行
jack
,只有两个条目具有非零值,因此排序不会起多大作用。根据t,有两对jaccard索引大于0.3的数据对,应该在输出中放在一起,但事实并非如此。“我离预期输出稍微近了一点”是毫无帮助的。请提供投入以及预期和实际产出。如果您尝试用文字描述排序算法也应该做什么,这将非常有用。另外,请检查您的变量名-它们当前非常糟糕。请检查我的编辑:)
范围(0,len(b)-1:
应该是
范围(len(b))
-
范围
不上升到但不包括
停止
参数。更好的是,采用我的答案建议的
枚举法。你的代码不适用于案例:t=[“cala”、“cala lima”、“uni”、“ali”、“uni le”、“ali po”、“tr”、“wq”、“tr uni”]编辑-这样更好吗?如果您提供了您期望的答案,而不是“不起作用”,这将是非常有帮助的。[“卡拉”、“卡拉利马”、“阿里”、“阿里波”、“uni”、“uni”、“uni le”、“tr uni”、“tr”、“wq”]检查mi edit,我离预期的输出稍微近了一点,也许您可以纠正我。您的代码很好,它非常接近预期的输出,但我不想重复,所以我选择第一次jaccard相似度大于0.3