You seem to be asking two separate questions. Based on your results:
000 001 011 111 101 100 010
Use a permutations_with_replacement combinatoric via itertools.product
.
111 12 21 3
Use a partitions algorithm via more_itertools.partitions
.
Given
import itertools as it
import more_itertools as mit
Code
# 1 - Permutation w Replacement
>>> list(it.product("01", repeat=3))
[('0', '0', '0'),
('0', '0', '1'),
('0', '1', '0'),
('0', '1', '1'),
('1', '0', '0'),
('1', '0', '1'),
('1', '1', '0'),
('1', '1', '1')]
# 2 - Partitions
>>> list(mit.partitions("111"))
[[['1', '1', '1']],
[['1'], ['1', '1']],
[['1', '1'], ['1']],
[['1'], ['1'], ['1']]]
Details
To achieve your specific results, use list comprehensions:
# 1
>>> ["".join(x) for x in it.product("01", repeat=3)]
['000', '001', '010', '011', '100', '101', '110', '111']
# 2
>>> [[sum(map(int, x)) for x in sub] for sub in mit.partitions("111")]
[[3], [1, 2], [2, 1], [1, 1, 1]]
more_itertools
is a third-party package. Install with > pip install more-itertools
.