## 生成所有排列，其中排列的长度> Python中的元素数内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (55)

``````[None, 0, 1, 2].
``````

``````(None, 0, None, None, 1).
``````

``````[state for state in list(set(it.permutations((None, None, None, 0, 0, 1, 1, 2, 2), 5))) if state.count(None)==3]
``````

### 2 个回答

``````def tien_gen(size, values, number_of_nones):
to_fill = size - number_of_nones
for locs in itertools.combinations(range(size), to_fill):
for fill_values in itertools.product(values, repeat=to_fill):
out = [None] * size
for loc, fill_value in zip(locs, fill_values):
out[loc] = fill_value
yield tuple(out)
``````

``````In [137]: result = list(tien_gen(5, [0,1,2], 3))

In [138]: len(result)
Out[138]: 90

In [139]: result
Out[139]:
[(0, 0, None, None, None),
(0, 1, None, None, None),
(0, 2, None, None, None),
(1, 0, None, None, None),
[...]
(None, 0, None, 1, None),
(None, 0, None, 2, None),
(None, 1, None, 0, None),
[...]
(None, 2, None, 1, None),
(None, 2, None, 2, None),
(None, 0, None, None, 0),
(None, 0, None, None, 1),
[...]
(None, None, None, 1, 2),
(None, None, None, 2, 0),
(None, None, None, 2, 1),
(None, None, None, 2, 2)]

In [140]: orig = [state for state in list(set(it.permutations((None, None, None, 0, 0, 1, 1, 2, 2), 5))) if state.count(None)==3]

In [141]: len(result) == len(orig) and set(result) == set(orig)
Out[141]: True
``````

``````def group(d, current = [], in_place = [None, 3]):
need, _occurs = in_place
if len(current) == 5 and current.count(need) == _occurs:
yield current
else:
for i in d:
_c = current+[i]
if len(_c) <= 5 and _c.count(need) <= _occurs:
yield from group(d, current = _c)
``````

``````[[None, None, None, 0, 0], [None, None, None, 0, 1], [None, None, None, 0, 2], [None, None, None, 1, 0], [None, None, None, 1, 1], [None, None, None, 1, 2], [None, None, None, 2, 0], [None, None, None, 2, 1], [None, None, None, 2, 2], [None, None, 0, None, 0], [None, None, 0, None, 1], [None, None, 0, None, 2], [None, None, 0, 0, None], [None, None, 0, 1, None], [None, None, 0, 2, None], [None, None, 1, None, 0], [None, None, 1, None, 1], [None, None, 1, None, 2], [None, None, 1, 0, None], [None, None, 1, 1, None], [None, None, 1, 2, None], [None, None, 2, None, 0], [None, None, 2, None, 1], [None, None, 2, None, 2], [None, None, 2, 0, None], [None, None, 2, 1, None], [None, None, 2, 2, None], [None, 0, None, None, 0], [None, 0, None, None, 1], [None, 0, None, None, 2], [None, 0, None, 0, None], [None, 0, None, 1, None], [None, 0, None, 2, None], [None, 0, 0, None, None], [None, 0, 1, None, None], [None, 0, 2, None, None], [None, 1, None, None, 0], [None, 1, None, None, 1], [None, 1, None, None, 2], [None, 1, None, 0, None], [None, 1, None, 1, None], [None, 1, None, 2, None], [None, 1, 0, None, None], [None, 1, 1, None, None], [None, 1, 2, None, None], [None, 2, None, None, 0], [None, 2, None, None, 1], [None, 2, None, None, 2], [None, 2, None, 0, None], [None, 2, None, 1, None], [None, 2, None, 2, None], [None, 2, 0, None, None], [None, 2, 1, None, None], [None, 2, 2, None, None], [0, None, None, None, 0], [0, None, None, None, 1], [0, None, None, None, 2], [0, None, None, 0, None], [0, None, None, 1, None], [0, None, None, 2, None], [0, None, 0, None, None], [0, None, 1, None, None], [0, None, 2, None, None], [0, 0, None, None, None], [0, 1, None, None, None], [0, 2, None, None, None], [1, None, None, None, 0], [1, None, None, None, 1], [1, None, None, None, 2], [1, None, None, 0, None], [1, None, None, 1, None], [1, None, None, 2, None], [1, None, 0, None, None], [1, None, 1, None, None], [1, None, 2, None, None], [1, 0, None, None, None], [1, 1, None, None, None], [1, 2, None, None, None], [2, None, None, None, 0], [2, None, None, None, 1], [2, None, None, None, 2], [2, None, None, 0, None], [2, None, None, 1, None], [2, None, None, 2, None], [2, None, 0, None, None], [2, None, 1, None, None], [2, None, 2, None, None], [2, 0, None, None, None], [2, 1, None, None, None], [2, 2, None, None, None]]
``````