【发布时间】:2021-11-12 13:37:16
【问题描述】:
我想生成一个所有 4 位数字排列的列表,其中:
- 始终存在所有 4 位数字
- 两个相反的序列是相同的解决方案。例如。 (1,2,3,4) = (4,3,2,1)
我想知道:
- 你怎么称呼这种排列。
- 如果可以一步生成此列表。下面是一个分两步生成它的示例。
import itertools
inp_list = range(1, 5)
# 1. Create the list of all permutations.
permutations = list(itertools.permutations(inp_list))
# 2. Remove sequences that are the reverse of another.
for _p in permutations[::-1]:
if _p[::-1] in permutations:
permutations.remove(_p)
for _p in permutations:
print("\t".join(map(str, _p)))
【问题讨论】:
标签: python permutation