通过切断循环来缩短元组列表?

时间:2016-08-09 23:49:09

标签: python list tuples

我有一个生成元组列表的函数,如:

[(0, 0), (1, 1), (1, 2), (1,3), (2, 4), (3, 5), (4, 5)]

用于表示我正在制作的游戏中的图块(行,列)的路径。

我用来生成这些路径的函数并不完美,因为它经常产生“循环”,如下所示:

[(2, 0), (2, 1), (1, 2), (0, 3), (0, 4), (1, 5), (2, 5), (3, 4), (3, 3),
 (3, 2), (4, 1)]

Image

上面的路径应该是:

[(2, 0), (2, 1), (3, 2), (4, 1)]

Image

这些路径可以包含任意数量的循环,可以是任何大小和形状。

所以我的问题是,如何在python中编写一个函数来切割循环列表并返回一个没有这些循环的新的更短的列表。

我的尝试如下:

def Cut_Out_Loops(Path):

    NewList = list(Path)
    Cutting = True

    a = 0
    for Cords in Path:
        a += 1

        try:
            for i in range(a + 2, len(Path)):
                if (Path[i][0] == Cords[0] and abs(Path[i][1] - Cords[1]) == 1:
                    NewList = NewList[0:a] + NewList[i:]
                    Path = list(NewList)
                elif Path[i][1] == Cords[1] and abs(Path[i][0] - Cords[0]) == 1:
                    NewList = NewList[0:a] + NewList[i:]
                    Path = list(NewList)
                elif abs(Path[i][0] - Cords[0]) == 1 and abs(Path[i][1] - Cords[1]) == 1:
                    NewList = NewList[0:a] + NewList[i:]
                    Path = list(NewList)
                elif abs(Path[i][1] - Cords[1]) == 1 and abs(Path[i][0] - Cords[0]) == 1:
                    NewList = NewList[0:a] + NewList[i:]
                    Path = list(NewList)
                Cutting = False
        except IndexError:
            Cutting = True

2 个答案:

答案 0 :(得分:2)

虽然你定义了一个"循环"不太清楚,试试这个

def clean(path):
    path1 = []
    for (x1,y1) in path:
        for (i,(x2,y2)) in enumerate(path1[:-1]):
            if abs(x1-x2) <= 1 and abs(y1-y2) <= 1:
                path1 = path1[:i+1]
                break
        path1.append((x1,y1))
    return path1

这绝对适用于您的示例:

 >>> path = [(2, 0), (2, 1), (1, 2), (0, 3), (0, 4), (1, 5), (2, 5), (3, 4), (3, 3), (3, 2), (4, 1)]
 >>> clean(path)
 [(2, 0), (2, 1), (3, 2), (4, 1)]

那就是说,它只是最直接的蛮力解决方案。复杂性是二次的。

答案 1 :(得分:1)

你的路径有多长?如果它们全部都在1000个元素之下,即使是一个天真的暴力算法也可以起作用:

path = [
    (2, 0),
    (2, 1),
    (1, 2),
    (0, 3),
    (0, 4),
    (1, 5),
    (2, 5),
    (3, 4),
    (3, 3),
    (3, 2),
    (4, 1)
]


def adjacent(elem, next_elem):
    return (abs(elem[0] - next_elem[0]) <= 1 and
            abs(elem[1] - next_elem[1]) <= 1)

new_path = []
i = 0
while True:
    elem = path[i]
    new_path.append(elem)
    if i + 1 == len(path):
        break
    j = len(path) - 1
    while True:
        future_elem = path[j]
        if adjacent(elem, future_elem):
            break
        j -= 1
    i = j

print new_path