python positive and negative number list possiblities -


i'm trying make function in python takes list of integers input , returns greater list containing positive , negative possibilities of numbers.

pretend '+' positive number , '-' negative number

the output should match with:

foo([-4]) >>> [ [4], [-4] ]  foo([+, +]) >>> [ [+,+], [+,-], [-,+], [-,-] ]  foo([-, +]) >>> [ [+,+], [+,-], [-,+], [-,-] ]  foo([-1, 3]) >>> [ [1,3], [1,-3], [-1,3], [-1,-3] ]  foo( [+,-,+] ) >>> [ [-,-,-],[+,-,-],[-,+,-],[-,-,+],[+,+,-],[+,-,+],[-,+,+],[+,+,+] ] 

for numbers, can use itertools.product create combos, after generating list both positive , negative numbers:

from itertools import product  def foo(nums):     return list(product(*((x, -x) x in nums))) 

demo:

>>> foo([-4]) [(4,), (-4,)] >>> foo([-1, 3]) [(1, 3), (1, -3), (-1, 3), (-1, -3)] >>> foo([1, 3]) [(1, 3), (1, -3), (-1, 3), (-1, -3)] >>> foo([1, -3, 4]) [(1, 3, 4), (1, 3, -4), (1, -3, 4), (1, -3, -4), (-1, 3, 4), (-1, 3, -4), (-1, -3, 4), (-1, -3, -4)] 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -