Reputation: 1257
I have 3 lists
i = ['a','b','c']
j = ['1','2','3']
l = ['01','02']
I want to create a dictionary looking something like this:
D = {'a':{'1':{'01':10, '02':100}, {'2':{'01':20,'02':50}}}
I want to be able to loop over i, j
and l
and do D[i][j][l]
to get the value associated with it.
Ds = {}
l_ = 0
for i_ in range(len(i)):
j_ = 0
for j_ in range(len(j)):
l_ = 0
for l_ in range(len(l)):
Ds[i[i_]] = {j[j_]:{l[l_]:'some value'}}
I tried this, but this does not work.
Extra:
I am writing a Linear program in Pulp
and I want to create a dictionary of demand values for a particular sku i
, in period j
on line l
so that I can do Dijl*Xijtl = some_value
summed over i
, j
,l
for constraints or something like that. These demand values are constants. This could be cartesian product or comprehension. Is there a function similar to LpVariable.dicts()
?
Upvotes: 1
Views: 2177
Reputation: 61
Since you are using PuLP, you may want to look into pulp's makeDict().
Suppose you have the following:
i = ['a', 'b']
j = ['1', '2', '3']
l = ['01', '02']
and that for your demand values you have:
value_a_1_01 = 10
value_a_1_02 = 20
value_a_2_01 = 30
value_a_2_02 = 40
value_a_3_01 = 50
value_a_3_02 = 60
value_b_1_01 = 70
value_b_1_02 = 80
value_b_2_01 = 90
value_b_2_02 = 100
value_b_3_01 = 110
value_b_3_02 = 120
You can then create an array that contains your demand data like this:
array = [[{'01': 10, '02': 20}, {'01': 30, '02': 40},
{'01': 50, '02': 60}],
[{'01': 70, '02': 80}, {'01': 90, '02': 100},
{'01': 110, '02': 120}]]
and now define your dictionary:
D = pulp.makeDict([i, j], array)
EDIT: Since the data set up was a bit messy before, setting up the array like this makes it much cleaner (and easier to edit):
array = [
[[10, 20], [30, 40], [50, 60]],
[[70, 80], [90, 100], [110, 120]]
]
Now the parameters for makeDict() should be:
D = pulp.makeDict([i, j, l], array)
Upvotes: 1
Reputation: 19810
I think this reads really nicely as a comprehension:
D = {i_: {j_: {l_: "value" for l_ in l} for j_ in j} for i_ in i}
Upvotes: 0
Reputation: 1059
Another way of doing this would just be to have a dictionary that is keyed by tuple values i.e
D ={}
for i in I:
for j in J:
for k in K:
D[i,j,k] = your value
Note in pulp
it would be more common to donate your sets with a capital letter and iterate with the smaller letter.
Upvotes: 0
Reputation: 46899
this is a variant using product
and setdefault
:
from itertools import product
D = {}
for ii, jj, ll in product(i, j, l):
D.setdefault(ii, {})
D[ii].setdefault(jj, {})
D[ii][jj][ll] = None
where i used None
as placeholder for your value.
Upvotes: 1
Reputation: 2096
If you want "Cartesian product" of lists, you can use:
i = ['a','b','c']
j = ['1','2','3']
l = ['01','02']
Ds = {}
for i_ in i:
item_i = {}
for j_ in j:
item_j = {}
for l_ in l:
item_j[l_] = '' # Your value
item_i[j_] = item_j
Ds[i_] = item_i
print Ds
# {'a': {'1': {'02': '', '01': ''}, '3': {'02': '', '01': ''}, '2': {'02': '', '01': ''}}, 'c': {'1': {'02': '', '01': ''}, '3': {'02': '', '01': ''}, '2': {'02': '', '01': ''}}, 'b': {'1': {'02': '', '01': ''}, '3': {'02': '', '01': ''}, '2': {'02': '', '01': ''}}}
Upvotes: 1