python - create list with negative value permutation -
can creating lists negative values out of given list?
for example:
values = [1, 2, 3]
desired output
[[-1, 2, 3], [1, -2, 3], [1, 2, -3],[-1, -2, 3], [1, -2, -3], [-1, 2, -3], [-1, -2, -3]]
produce product of 3 times [1, -1]
, multiply result input list:
from itertools import product values = [1, 2, 3] [[num * mul num, mul in zip(values, combo)] combo in product([1, -1], repeat=len(values))]
demo:
>>> itertools import product >>> values = [1, 2, 3] >>> [[num * mul num, mul in zip(values, combo)] combo in product([1, -1], repeat=len(values))] [[1, 2, 3], [1, 2, -3], [1, -2, 3], [1, -2, -3], [-1, 2, 3], [-1, 2, -3], [-1, -2, 3], [-1, -2, -3]]
the output order different specified, same values produced, plus original list first element.
Comments
Post a Comment