你可以试试下面的代码。在import collections, numpy
tokens=['He','is','not','lazy','intelligent','smart']
j=0
a=np.zeros((len(tokens),len(tokens)))
for pos,token in enumerate(tokens):
j+=pos+1
for token1 in tokens[pos+1:]:
count = 0
for sentence in [['He','is','not','lazy','He','is','intelligent','He','is','smart'] ]:
occurrences1 = [i for i,e in enumerate(sentence) if e == token1]
#print(token1,occurrences1)
occurrences2 = [i for i,e in enumerate(sentence) if e == token]
#print(token,occurrences2)
new1= np.repeat(occurrences1,len(occurrences2))
new2= np.asarray(occurrences2*len(occurrences1))
final_new= np.subtract(new1,new2)
final_abs_diff = np.absolute(final_new)
final_counts = collections.Counter(final_abs_diff)
count_1=final_counts[1]
count_2=final_counts[2]
count_0=final_counts[0]
count=count_1+count_2+count_0
a[pos][j]=count
#print(token,' ',pos,' ',token1,' ',j,' ',count)
j+=1
j=0
final_mat = a.T+a
print(final_mat)
输出为:
^{pr2}$