簡體   English   中英

元組列表的所有可能組合

[英]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.combinationsitertools.permutations但它似乎沒有完成這項工作,例如以下路徑:

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

沒有出現在列表中。

這個特定的 function 不一定必須是 output 的“有效”路徑(有效 = 每次水平、垂直或對角移動一步),只是代表棋盤的元組中的所有可能組合。 我有一個 function 來檢查某個路徑是否返回有效單詞。 我正在嘗試打印出在板上返回有效單詞的所有可能路徑。

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)]
(...)

itertools.product()完全符合您的要求。

>>> import itertools
>>> tuple1 = ("A", "B", "C")
>>> tuple2 = ("D", "E", "F")
>>> list(itertools.product(tuple1, tuple2))
[('A', 'D'), ('A', 'E'), ('A', 'F'), ('B', 'D'), ('B', 'E'), ('B', 'F'), ('C', 'D'), ('C', 'E'), ('C', 'F')]
>>> [''.join(x) for x in itertools.product(tuple1, tuple2)]
['AD', 'AE', 'AF', 'BD', 'BE', 'BF', 'CD', 'CE', 'CF']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM