【问题标题】:segmentation of elements of a list into sub-groups according to the distance between it根据列表元素之间的距离将列表元素分割为子组
【发布时间】:2021-07-14 16:18:31
【问题描述】:

如何使用python根据元素之间的距离(欧几里得距离,其中索引被认为是角度)将列表(距离列表)的元素细分为组,如下例所示。新列表必须从原始列表中的起始索引开始

PS: 在欧几里得距离中,我们需要一个角度来计算它。所以我认为角度是列表的索引,例如 list[0]=10 和 list1=12 来计算我们首先需要得到每个值的 X 和 Y 的欧几里得距离

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)) 

【问题讨论】:

  • 你能解释更多吗?我无法理解。我知道欧几里得距离是多少。但是索引和值之间的关系呢?
  • 你在纸上试过了吗?有时最好先在纸上计算算法。我有点怀疑你在这里写的东西。 @Zahra
  • 是的,我做到了......但我找不到将每个值放在不同向量中的想法

标签: python python-3.x list euclidean-distance


【解决方案1】:

希望这可行:

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)

结果:

[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]

【讨论】:

  • Sir @Soroosh 为了删除值 0 我添加了 if val !=0 但在第一次迭代中第一种情况没有得到正确的索引 2873.25, 4187.5, 3690.75, 2871.0, 2883.5, 2896.75, 3494.5, 0, 0, 0, 0, 0, 0, 4544.5, 4420.75, 4330.0, 4525.0 结果是 [[0, 2873.25, 2913.5, 2863.25, 2874.75, 2868.5, 2873.25, 4187.5, 3690.75, 2871.0, 2883.5, 2896.75, 3494.5], [12, 4544.5, 4420.75, 4330.0, 4525.0]] 但我应该获取[[0, 2873.25, 2913.5, 2863.25, 2874.75, 2868.5, 2873.25, 4187.5, 3690.75, 2871.0, 2883.5, 2896.75, 3494.5], [18, 4544.5, 4420.75, 4330.0, 4525.0]]
  • 你的意思是16? @Zahra
  • 还有@Zahra 你用 9 代替什么?我知道 9 在你的新数据集中不能作为最后一个。新的距离限制是多少?
  • 我用1700作为距离
  • 我希望这将是你想要的@Zahra:给定[2873.25, 4187.5, 3690.75, 2871.0, 2883.5, 2896.75, 3494.5, 0, 0, 0, 0, 0, 0, 4544.5, 4420.75, 4330.0, 4525.0] 它会返回:[[0, 2873.25, 4187.5, 3690.75, 2871.0, 2883.5, 2896.75, 0], [13, 4544.5, 4420.75, 4330.0, 4525.0]] 可以吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
相关资源
最近更新 更多