I'm currently working on a HC single linkage method in Python and when I'm cluster each element I'm storing the current clusters as:
array1=[(0, 1), (3, 5), 2, 4]
elements1=len(array1[0])
array2[((3, 5), 4), (0, 1), 2]
elements2=len(array2[0])
What I want to do is find the number of elements in the zero'th index of each array, for the first elements1 I get the correct answer of 2 but for elements2 I also get 2 but it should be 3, I can see the problem however I can't seem to be able to work around
CodePudding user response:
In array2, the first index has a tuple of size 2, the first element is another tuple (3, 5)
, and the second element is a single int 4
If you want len(array2[0])
to return 3, you have to unpack all the values present at that index.
That can be done by the following code:
length = len(array2[0])
if isinstance(array2[0][0], tuple):
length -= 1
length = len(array2[0][0])
# replacing one with the size of the tuple
You can do this in a loop to check for all instances.
I tried to find a method to unpack the values you have, but that did not work, this was the next best solution.
Let me know if it works!