gpt4 book ai didi

python - 如何找到哪些列表是对称/翻译的?

转载 作者:行者123 更新时间:2023-12-04 10:39:21 25 4
gpt4 key购买 nike

下图有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]]
]


enter image description here

最佳答案

您可以通过以下方式找到答案:

  • 定义您的列表
  • 对于每个列表,检查它是否与之前的任何唯一列表匹配。
  • 每个列表有 4 个可能的镜像版本(2 个沿 x 和 2 个沿
    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

    关于python - 如何找到哪些列表是对称/翻译的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60007104/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com