I have a integer list say l1=[a,b,c] and _1to9=range(1,10). I’d like to get this:
[a*i1+b*i2+c*i3 for i1 in _1to9 for i2 in _1to9 for i3 in _1to9]
but the problem is that the l1 is not necessarily a list of 3 elements. so how do I generalize?
EDIT: to help visualize what I’m trying to achieve:
>>> l1=[10001,1010, 100]
>>> [l1[0]+i1+l1[1]*i2+l1[2]*i3 for i1 in _1to9 for i2 in _1to9 for i3 in _1to9]
Some basic math might help here. First, realise that
a*i1+b*i2+c*i3is the inner (dot) product of two three-element lists, which can be generalized toand
for i1 in _1to9 for i2 in _1to9 for i3 in _1to9loops over the Cartesian product of[_1to9] * 3. That’s in the Python standard library asitertools.product, so you’ve gotGeneralizing that to arbitrary lists
lgives