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

Python 如何找到对称/转换的列表?,python,python-3.x,Python,Python 3.x,下图有6幅从a到f的图片,每幅图片都描述了点a、B、C、D、E,点位置为二维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.37

下图有6幅从
a
f
的图片,每幅图片都描述了点
a
B
C
D
E
,点位置为二维
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]]
]

您可以找到以下答案:

  • 定义你的列表
  • 对于每个列表,检查它是否与以前的任何唯一列表匹配
  • 每个列表有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
    

    那么你的输入是图片还是数据点?另外,请看@anishtain,这些图片描述了数据点。请在你能想象他们的安排后,才关注数据点。非常感谢!我只是不清楚命令``if sum([1代表I in xs if I>=meanx])>len(lst)//2:max_x=lst[-1][0]lst=[[-(I[0]-max_x),I[1]]代表I in lst]`@BinhThien我已经实现了一种更好的方法,它应该没有未发现的边缘情况。我还认为直观理解要容易得多