I have this list:
t=[['universitario de deportes'],['lancaster'],['universitario de'],['juan aurich'],['muni'],['juan']]
I want to reorder the list according to the jaccard distance. If I reorder t
the expected ouput should be:
[['universitario de deportes'],['universitario de'],['lancaster'],['juan aurich'],['juan'],['muni']]
The code of the jackard distance is working OK, but the rest of the code doesn't give the expected output.The code is below:
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]
Unfortunately, the unexpected output is the same list :
print c
['universitario de deportes', 'lancaster', 'universitario de', 'juan aurich', 'muni', 'juan']
EDIT:
I tried to correct my code but it didn't work too but I got a little closer to the expected output:
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]
The "close" output is:
['universitario de deportes', 'universitario de', 'lancaster', 'juan aurich', 'muni', 'juan']
Second edit:
Finally, I came up with a solution that has quite fast computational. Currently, I'll use the code to order 60 thousands names. The code is below:
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'
Firstly, not sure why you've got everything in single-item lists, so I suggest flattening it out first:
t = [l[0] for l in t]
This gets rid of the extra zero indices everywhere, and means you only need shallow copies (as strings are immutable).
Secondly, the last three lines of your code never run:
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]
I think what you want is:
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
This gives me
out == ['universitario de deportes', 'universitario de',
'lancaster', 'juan aurich', 'juan', 'muni']
I would generally recommend using better variable names than a
, b
, c
, etc..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.