在找到eulidean距离时,我得到TypeError:不支持的操作数类型 - :'list'和'list'

时间:2022-06-25 20:03:44

I get an error: TypeError: unsupported operand type(s) for -: 'list' and 'list' while finding Euclidean Distance of my chromosomes. Here is my code:

我收到一个错误:TypeError:不支持的操作数类型 - :'list'和'list',同时找到我的染色体的欧几里德距离。这是我的代码:

organisms=[canis_lupus,Drosophila_melanogaster,Felis_catus,Homo_sapiens,Mus_musculus]

def euclidean_distance(p,q):
   distance = np.linalg.norm(p-q)
   return distance

edistance = []
for i in range(len(organisms)):
  for j in range(i+1,len(organisms)):
     edistance.append(euclidean_distance(organisms[i],organisms[j]))

print(edistance) 

Please note that if I print organisms, each element is basically a list of length 256.

请注意,如果我打印有机体,每个元素基本上都是长度为256的列表。

1 个解决方案

#1


1  

p-q is interpreted as a list subtraction (since p and q are lists), which is not defined. Convert your lists to np.arrays first:

p-q被解释为列表减法(因为p和q是列表),未定义。首先将列表转换为np.arrays:

distance = np.linalg.norm(np.array(p)-np.array(q))

#1


1  

p-q is interpreted as a list subtraction (since p and q are lists), which is not defined. Convert your lists to np.arrays first:

p-q被解释为列表减法(因为p和q是列表),未定义。首先将列表转换为np.arrays:

distance = np.linalg.norm(np.array(p)-np.array(q))