元组列表的所有可能组合
All possible combinations of a list of tuples
我正在 python 中创建“Boggle”,并且我有一个表示游戏板上坐标的元组列表:
all_coordinates = [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3)]
我正在尝试创建一个新的元组列表列表,它将代表板上所有可能的路径。
看起来像这样:
[[(0,0),(1,0)], ... , [(0,0),(1,0),(2,0),(2,1)] , ... , [(2, 1), (2, 2), (2, 3)], ...]
我尝试使用 itertools.combinations
和 itertools.permutations
,但它似乎无法完成工作,例如以下路径:
[(2,1),(1,1),(1,0),(2,0)]
没有出现在列表中。
这个特定的函数不一定要输出 'valid' 路径(有效 = 每次水平、垂直或对角线移动一步),只是代表棋盘的元组的所有可能组合。我有一个函数可以检查某个路径 return 是否是一个有效的词。我正在尝试打印出所有可能的路径 return 黑板上的一个有效单词。
itertools.permutations
确实产生了所有排列,包括您所说的 [(2,1),(1,1),(1,0),(2,0)]
缺失的排列。请注意,每次调用 permutations
都会获得特定长度的所有排列:
>>> all_coordinates = [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3)]
>>> from itertools import permutations
>>> ((2,1),(1,1),(1,0),(2,0)) in permutations(all_coordinates, 4)
True
如果您想查看从长度 2 到长度 4 的所有排列,请尝试:
for k in range(2, 5):
for p in permutations(all_coordinates, k):
print(p)
生成的序列很长;正如其他人指出的那样,您可能想想出另一种方法来生成 仅 包含相邻坐标的路径(例如广度优先搜索)。
(edit) 只是为了好玩,这是一种非常快速的 DFS 方法,它通过仅查看相邻坐标来构建长度不超过 4 的所有路径:
>>> def print_paths(path):
... print(path)
... if len(path) >= 4:
... return
... x, y = path[-1]
... for dx in range(-1, 2):
... for dy in range(-1, 2):
... c = x + dx, y + dy
... if c not in path and c in all_coordinates:
... print_paths(path + [c])
...
>>> print_paths([(2, 1)])
[(2, 1)]
[(2, 1), (1, 0)]
[(2, 1), (1, 0), (0, 0)]
[(2, 1), (1, 0), (0, 0), (0, 1)]
[(2, 1), (1, 0), (0, 0), (1, 1)]
[(2, 1), (1, 0), (0, 1)]
[(2, 1), (1, 0), (0, 1), (0, 0)]
[(2, 1), (1, 0), (0, 1), (0, 2)]
[(2, 1), (1, 0), (0, 1), (1, 1)]
[(2, 1), (1, 0), (0, 1), (1, 2)]
[(2, 1), (1, 0), (1, 1)]
[(2, 1), (1, 0), (1, 1), (0, 0)]
[(2, 1), (1, 0), (1, 1), (0, 1)]
[(2, 1), (1, 0), (1, 1), (0, 2)]
[(2, 1), (1, 0), (1, 1), (1, 2)]
[(2, 1), (1, 0), (1, 1), (2, 0)]
[(2, 1), (1, 0), (1, 1), (2, 2)]
[(2, 1), (1, 0), (2, 0)]
[(2, 1), (1, 0), (2, 0), (1, 1)]
[(2, 1), (1, 1)]
[(2, 1), (1, 1), (0, 0)]
[(2, 1), (1, 1), (0, 0), (0, 1)]
[(2, 1), (1, 1), (0, 0), (1, 0)]
[(2, 1), (1, 1), (0, 1)]
[(2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 1), (1, 1), (0, 1), (0, 2)]
[(2, 1), (1, 1), (0, 1), (1, 0)]
[(2, 1), (1, 1), (0, 1), (1, 2)]
[(2, 1), (1, 1), (0, 2)]
[(2, 1), (1, 1), (0, 2), (0, 1)]
[(2, 1), (1, 1), (0, 2), (0, 3)]
[(2, 1), (1, 1), (0, 2), (1, 2)]
[(2, 1), (1, 1), (0, 2), (1, 3)]
[(2, 1), (1, 1), (1, 0)]
[(2, 1), (1, 1), (1, 0), (0, 0)]
[(2, 1), (1, 1), (1, 0), (0, 1)]
[(2, 1), (1, 1), (1, 0), (2, 0)]
(...)
我正在 python 中创建“Boggle”,并且我有一个表示游戏板上坐标的元组列表:
all_coordinates = [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3)]
我正在尝试创建一个新的元组列表列表,它将代表板上所有可能的路径。
看起来像这样:
[[(0,0),(1,0)], ... , [(0,0),(1,0),(2,0),(2,1)] , ... , [(2, 1), (2, 2), (2, 3)], ...]
我尝试使用 itertools.combinations
和 itertools.permutations
,但它似乎无法完成工作,例如以下路径:
[(2,1),(1,1),(1,0),(2,0)]
没有出现在列表中。
这个特定的函数不一定要输出 'valid' 路径(有效 = 每次水平、垂直或对角线移动一步),只是代表棋盘的元组的所有可能组合。我有一个函数可以检查某个路径 return 是否是一个有效的词。我正在尝试打印出所有可能的路径 return 黑板上的一个有效单词。
itertools.permutations
确实产生了所有排列,包括您所说的 [(2,1),(1,1),(1,0),(2,0)]
缺失的排列。请注意,每次调用 permutations
都会获得特定长度的所有排列:
>>> all_coordinates = [(0, 0), (1, 0), (2, 0), (0, 1), (1, 1), (2, 1), (0, 2), (1, 2), (2, 2), (0, 3), (1, 3), (2, 3)]
>>> from itertools import permutations
>>> ((2,1),(1,1),(1,0),(2,0)) in permutations(all_coordinates, 4)
True
如果您想查看从长度 2 到长度 4 的所有排列,请尝试:
for k in range(2, 5):
for p in permutations(all_coordinates, k):
print(p)
生成的序列很长;正如其他人指出的那样,您可能想想出另一种方法来生成 仅 包含相邻坐标的路径(例如广度优先搜索)。
(edit) 只是为了好玩,这是一种非常快速的 DFS 方法,它通过仅查看相邻坐标来构建长度不超过 4 的所有路径:
>>> def print_paths(path):
... print(path)
... if len(path) >= 4:
... return
... x, y = path[-1]
... for dx in range(-1, 2):
... for dy in range(-1, 2):
... c = x + dx, y + dy
... if c not in path and c in all_coordinates:
... print_paths(path + [c])
...
>>> print_paths([(2, 1)])
[(2, 1)]
[(2, 1), (1, 0)]
[(2, 1), (1, 0), (0, 0)]
[(2, 1), (1, 0), (0, 0), (0, 1)]
[(2, 1), (1, 0), (0, 0), (1, 1)]
[(2, 1), (1, 0), (0, 1)]
[(2, 1), (1, 0), (0, 1), (0, 0)]
[(2, 1), (1, 0), (0, 1), (0, 2)]
[(2, 1), (1, 0), (0, 1), (1, 1)]
[(2, 1), (1, 0), (0, 1), (1, 2)]
[(2, 1), (1, 0), (1, 1)]
[(2, 1), (1, 0), (1, 1), (0, 0)]
[(2, 1), (1, 0), (1, 1), (0, 1)]
[(2, 1), (1, 0), (1, 1), (0, 2)]
[(2, 1), (1, 0), (1, 1), (1, 2)]
[(2, 1), (1, 0), (1, 1), (2, 0)]
[(2, 1), (1, 0), (1, 1), (2, 2)]
[(2, 1), (1, 0), (2, 0)]
[(2, 1), (1, 0), (2, 0), (1, 1)]
[(2, 1), (1, 1)]
[(2, 1), (1, 1), (0, 0)]
[(2, 1), (1, 1), (0, 0), (0, 1)]
[(2, 1), (1, 1), (0, 0), (1, 0)]
[(2, 1), (1, 1), (0, 1)]
[(2, 1), (1, 1), (0, 1), (0, 0)]
[(2, 1), (1, 1), (0, 1), (0, 2)]
[(2, 1), (1, 1), (0, 1), (1, 0)]
[(2, 1), (1, 1), (0, 1), (1, 2)]
[(2, 1), (1, 1), (0, 2)]
[(2, 1), (1, 1), (0, 2), (0, 1)]
[(2, 1), (1, 1), (0, 2), (0, 3)]
[(2, 1), (1, 1), (0, 2), (1, 2)]
[(2, 1), (1, 1), (0, 2), (1, 3)]
[(2, 1), (1, 1), (1, 0)]
[(2, 1), (1, 1), (1, 0), (0, 0)]
[(2, 1), (1, 1), (1, 0), (0, 1)]
[(2, 1), (1, 1), (1, 0), (2, 0)]
(...)