如何找到对称/转换的列表?

2024-09-24 10:25:32 发布

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

下图有6幅从a)f)的图片,每幅图片都描绘了点ABCDE的二维点位置a)c)形状像梯形,而b)d)看起来像V字符。而且,e)f)具有相同的点排列。我想收集3组:

(a, c)
(b, d)
(e, f)
a = [
[[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.250, 0.500], [0.375, 0.250]],
[[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.375, 0.250], [0.500, 0.500]],
[[0.375, 0.250], [0.500, 0.000], [0.500, 0.500], [0.625, 0.250], [0.750, 0.500]],
[[0.375, 0.250], [0.500, 0.500], [0.625, 0.750], [0.750, 0.500], [0.875, 0.250]],
[[0.000, 0.000], [0.250, 0.500], [0.500, 0.000], [0.500, 0.500], [0.750, 0.500]],
[[0.125, 0.250], [0.375, 0.250], [0.375, 0.750], [0.625, 0.250], [0.875, 0.750]]
]

输出:

[
[[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.250, 0.500], [0.375, 0.250]],
[[0.375, 0.250], [0.500, 0.000], [0.500, 0.500], [0.625, 0.250], [0.750, 0.500]]
]
================
[
[[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.375, 0.250], [0.500, 0.500]],
[[0.375, 0.250], [0.500, 0.500], [0.625, 0.750], [0.750, 0.500], [0.875, 0.250]]
]
================
[
[[0.000, 0.000], [0.250, 0.500], [0.500, 0.000], [0.500, 0.500], [0.750, 0.500]],
[[0.125, 0.250], [0.375, 0.250], [0.375, 0.750], [0.625, 0.250], [0.875, 0.750]]
]

enter image description here


Tags: 图片字符形状梯形
1条回答
网友
1楼 · 发布于 2024-09-24 10:25:32

您可以找到以下答案:

  1. 定义你的列表
  2. 对于每个列表,检查它是否与以前的任何唯一列表匹配
  3. 每个列表有4个可能的镜像版本(2个沿x和2个沿x) y) 所以,检查所有4个

步骤1:

a = [[[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.250, 0.500], [0.375, 0.250]],
     [[0.000, 0.500], [0.125, 0.250], [0.250, 0.000], [0.375, 0.250], [0.500, 0.500]],
     [[0.375, 0.250], [0.500, 0.000], [0.500, 0.500], [0.625, 0.250], [0.750, 0.500]],
     [[0.375, 0.250], [0.500, 0.500], [0.625, 0.750], [0.750, 0.500], [0.875, 0.250]],
     [[0.000, 0.000], [0.250, 0.500], [0.500, 0.000], [0.500, 0.500], [0.750, 0.500]],
     [[0.125, 0.250], [0.375, 0.250], [0.375, 0.750], [0.625, 0.250], [0.875, 0.750]]]

步骤2:

graphs = []
for i in range(len(a)):
     matched = False
     for j in range(len(graphs)):
          if compare_mirrored_lists(graphs[j][0], a[i]):
               graphs[j].append(a[i])
               matched = True
               break
     if not matched:
          # If no matches were found, it's a new list and can be added
          graphs.append([a[i]])

步骤3:

def set_at_origin(lst):
    # Make sure the list will always have the same order
    lst = sorted(lst)
    # Make sure the first element is at [0, 0]
    return [[i[0] - lst[0][0], i[1] - lst[0][1]] for i in lst]

def compare_lists(a, b, error=0.001):
    # Check if a list of lists is the same
    for i in range(len(a)):
        for j in range(len(a[i])):
            if abs(a[i][j] - b[i][j]) > error:
                return False
    return True

def compare_mirrored_lists(list_1, list_2):
    list_1 = set_at_origin(list_1)
    list_2 = set_at_origin(list_2)

    # Check all 4 mirrored options
    for mirror_x in [-1, 1]:
        for mirror_y in [-1, 1]:
            list_2_mirrored = [[mirror_x * i[0], mirror_y * i[1]] for i in list_2]
            list_2_mirrored = set_at_origin(list_2_mirrored)

            if compare_lists(list_1, list_2_mirrored):
                return True

    return False

相关问题 更多 >