【问题标题】:All possible combinations of a list of tuples元组列表的所有可能组合
【发布时间】:2022-01-01 17:46:59
【问题描述】:

我正在 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.combinationsitertools.permutations,但它似乎无法完成这项工作,例如以下路径:

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

没有出现在列表中。

此特定函数不一定必须输出“有效”路径(有效 = 每次水平、垂直或对角移动一步),只需输出代表棋盘的元组的所有可能组合。我有一个函数可以检查某个路径是否返回有效单词。我正在尝试打印出所有可能在板上返回有效单词的路径。

【问题讨论】:

  • 你有没有看itertools.permutations,它和combinations很像,但顺序不同?
  • 这能回答你的问题吗? How to generate all permutations of a list?
  • 我确实尝试过使用itertools.permutations,但它并没有解决我的问题。我现在将编辑问题。
  • 然后解释你想要的输出背后的逻辑是什么
  • 我不推荐这个。您的元组的可能顺序的数量可能非常巨大。只需生成并测试有效路径。

标签: python list path combinations itertools


【解决方案1】:

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)

生成的序列很长;正如其他人指出的那样,您可能想想出另一种方法来生成包含相邻坐标的路径(例如广度优先搜索)。

(编辑)只是为了好玩,这是一种非常快速的 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)]
(...)

【讨论】:

  • 谢谢,这回答了我的问题,尽管我似乎必须按照你说的那样尝试另一种方法。
  • 有趣deltaless version。顺便说一句,您的 ... 相当不方便。
猜你喜欢
  • 2023-02-14
  • 2011-12-09
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多