Reputation: 391
How to subdivide the elements of the list (list of distance ) into groups according to the distance (euclidean-distance where the indices is supposed as angle) between the elements using python like in the example below. where the new lists must start with the starting index in the original list
PS: in the Euclidean distance, we need an angle to calculate it. So I supposed that the angle is the indices of list for example list[0]=10 and list1=12 to calculate the Euclidean distance we need first to get X and Y of each value
x1 = (list[i] * math.cos(math.radians(float(i))))
y1 = (list[i] * math.sin(math.radians(float(i))))
x2 = (list[i+1] * math.cos(math.radians(float(i+1))))
y2= (list[i+1] * math.sin(math.radians(float(i+1))))
distance= sqrt((x1 - x2) * (X - x2) + (posYd - y2) * (posYd - y2))
Upvotes: 2
Views: 274
Reputation: 468
Hope this works :
import math
# initializing
result = []
vals = [10, 12, 11.5, 12.5, 135, 132, 133, 136, 2, 3, 4, 59, 60, 61, 66, 65, 63, 100, 102, 100]
max_distance = 9
temp = [0] # Always the first index is zero
for i, val in enumerate(vals):
if i != len(vals)-1:
x1 = vals[i] * math.cos(math.radians(float(i)))
y1 = vals[i] * math.sin(math.radians(float(i)))
x2 = vals[i + 1] * math.cos(math.radians(float(i + 1)))
y2 = vals[i + 1] * math.sin(math.radians(float(i + 1)))
distance = math.sqrt((x1 - x2) ** 2 + (y2 - y1) ** 2)
# Append the non-zero values
if int(val) != 0:
temp.append(val)
if val != 0 and (distance > max_distance or vals[i+1] == 0):
# There is 2 condition for ending a cluster
# if present value !=0 and
# con 1 : distance > max_distance
# con 2 : the next value == 0
result.append(temp)
if vals[i+1] != 0 and (distance > max_distance or val == 0):
# There is 2 condition for start a new cluster
# if next value !=0 and
# con 1 : distance > max_distance
# con 2 : the present value == 0
temp = [i + 1]
else:
last = val
temp.append(last) # The final member
result.append(temp) # The final cluster
for ls in result:
print(ls)
The result :
[0, 10, 12, 11.5, 12.5]
[4, 135, 132, 133, 136]
[8, 2, 3, 4]
[11, 59, 60, 61, 66, 65, 63]
[17, 100, 102, 100]
Upvotes: 1