Don't forget to format your code using the triple backticks or tildes:

``````Here is my schema:

```sql
CREATE TABLE article_to_warehouse (
article_id   INTEGER
, warehouse_id INTEGER
)
;
```

How do I add a `UNIQUE` constraint?
``````

1. [3]
Bonooru
(edited )
I've got an algorithm that's in exponential time (ugh) and I can't seem to find a clever way around that. I've got a few optimizations that should let me abort early if there are bad combinations,...

I've got an algorithm that's in exponential time (ugh) and I can't seem to find a clever way around that. I've got a few optimizations that should let me abort early if there are bad combinations, but that's a separate issue. Anyone know a way to write for-loop chain that's depth is dependent on imported data in python?

I'm trying to avoid having to write nasty code like:

``````for aaa in [1,2,3]:
for bbb in [1,2,3]:
for ccc in [1,2,3]:
⋮
for zzz in [1,2,3]:
my_function(aaa,bbb,ccc, ... , zzz)
``````

Especially since I don't know exactly how many members the for loop chain will have.

EDIT:
The more I'm thinking about it, it should be trivial to calculate if I can generate every base-k number (where k is the number of options) of length n (where n is the length of my input). I think I can then write a function that takes that number as input, converts the numbers into the different choices and checks for optimizations and then runs `my_function`

1. [2]
onyxleopard
(edited )
I guess my first question is, for the values that you are iterating over in each loop, are they constant as in your example? If so, it seems like your innermost my_function could just be called on...

I guess my first question is, for the values that you are iterating over in each loop, are they constant as in your example?

If so, it seems like your innermost `my_function` could just be called on the product of `[[1,2,3]] * depth` (where `depth` is 24 in the case of aaa ... zzz). You could write this using `itertools.product` like so:

``````from itertools import product

arg_values = [1,2,3]
depth = 3
args_list = list(product(*([arg_values] * depth))) # → [
# (1, 1, 1),
# (1, 1, 2),
# (1, 1, 3),
# (1, 2, 1),
# (1, 2, 2),
# (1, 2, 3),
# (1, 3, 1),
# (1, 3, 2),
# (1, 3, 3),
# (2, 1, 1),
# (2, 1, 2),
# (2, 1, 3),
# (2, 2, 1),
# (2, 2, 2),
# (2, 2, 3),
# (2, 3, 1),
# (2, 3, 2),
# (2, 3, 3),
# (3, 1, 1),
# (3, 1, 2),
# (3, 1, 3),
# (3, 2, 1),
# (3, 2, 2),
# (3, 2, 3),
# (3, 3, 1),
# (3, 3, 2),
# (3, 3, 3)
# ]

for args in args_list:
my_function(*args)
``````

Edit:

If you want to avoid allocating the `list` for the intermediate `args_list`, try consuming the generated args from `product` directly:

``````for args in product(*([arg_values] * depth)):
my_function(*args)
``````