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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -