python组浮点值

2024-04-19 02:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有0到1范围内点的x和y值,我想用彼此更接近的值对它们进行分组,但首先应根据y值对它们进行分组,然后我想根据x值在每组中创建子组

我的价值观是

y=[0.20854389667510986, 0.21354246139526367, 0.2514365017414093, 0.2824919819831848, 
     0.28884729743003845, 0.2938481867313385, 0.29938602447509766, 0.3079403340816498, 
     0.3110620379447937]
x=[0.19389697909355164, 0.34383803606033325, 0.42771604657173157, 0.5027003884315491, 
     0.27740684151649475, 0.5065063238143921, 0.5693719983100891, 0.6398013830184937, 
     0.7191168665885925]

有什么帮助吗

已编辑

下面是图像示例,绿色是我需要的数字,红色是不需要的数字 enter image description here

enter image description here

下面是我目前使用的代码

objs = [{'box': (0.42659902572631836, 0.7797865271568298, 0.8791541457176208, 0.8509170413017273), 'class_name': '4',
     'score': 0.99999213},
    {'box': (0.38698509335517883, 0.47336792945861816, 0.8555437922477722, 0.5579431056976318), 'class_name': '4',
     'score': 0.9999858},
    {'box': (0.41911759972572327, 0.6990442872047424, 0.8730689287185669, 0.775518000125885), 'class_name': '2',
     'score': 0.9999598},
    {'box': (0.42790335416793823, 0.8458782434463501, 0.8958504796028137, 0.9237207174301147), 'class_name': '8',
     'score': 0.99989796},
    {'box': (0.3866911232471466, 0.6215671896934509, 0.8674313426017761, 0.6978859305381775), 'class_name': '6',
     'score': 0.9997212},
    {'box': (0.4925450384616852, 0.27575790882110596, 0.8116368651390076, 0.32022011280059814), 'class_name': '1',
     'score': 0.9975745},
    {'box': (0.37777039408683777, 0.551058292388916, 0.8532172441482544, 0.6227307319641113), 'class_name': '5',
     'score': 0.99741983},
    {'box': (0.40477627515792847, 0.9192683696746826, 0.8955397009849548, 0.9945260286331177), 'class_name': '5',
     'score': 0.9972119},
    {'box': (0.4721110463142395, 0.22912952303886414, 0.8121180534362793, 0.3223329484462738), 'class_name': '4',
     'score': 0.8815911}]
sorted_objs = sorted(objs, key=lambda obj: obj['box'][0])
groups = [[]]
active = 0
min_distance = 0.03
i = 0
groups[active].append(sorted_objs[i])
i+=1
while i < len(sorted_objs):
   if abs(sorted_objs[i-1]['box'][0] - sorted_objs[i]['box'][0]) <=min_distance:
       groups[active].append(sorted_objs[i])
   else:
       groups.append([])
       active += 1
       groups[active].append(sorted_objs[i])
   i += 1
print([box['class_name'] for box in sorted(sorted(groups,key=lambda key:len(key))[-1], key=lambda key: key['box'][1])])

在上面的代码中,我使用固定的最小距离,但我希望它更加动态


Tags: lambdakey代码nameboxobj数字min