I'm new to python. I have the following task to iterate all pairs of the values of a dictionary to calculate a value (hamming distance of the two sequences (each value pair is a sequence pair)). Then, I need to print out the corresponding keys if the hamming distance calculated is 1. The code is as follows.
import numpy as np
import itertools
from scipy.spatial.distance import hamming
graph=[]
i=5
t1=tuple('aaaaa')
t2=tuple('aaaab')
t3=tuple('aaaac')
t4=tuple('aaaad')
t5=tuple('aaaaa')
population={'1':t1, '2':t2, '3':t3, '4':t4, '5':t5}
for pair in itertools.combinations(np.array(population.values()),2):
if hamming(pair[0],pair[1])*i==1: graph.append(str(population.keys()[population.values().index(pair[0])]) +'\t' + str(population.keys()[population.values().index(pair[1])]) +'n')
print graph
The error is:
Traceback (most recent call last):
File "test.py", line 18, in <module>
if hamming(pair[0],pair[1])*len==1: graph.append(str(population.keys()[population.values().index(pair[0])]) +'\t' + str(population.keys()[population.values().index(pair[1])]) +'n')
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Any comment is greatly appreciated. Edit: I learned to access the key from the value of a dictionary from this link: Get key by value in dictionary Edit: to avoid the variable to be built-in.