Reputation: 35
the following problem may be a bit confusing but I will try to explain it in the best possible way.
Let's assume that we work for a manufacturing company. Which manufactures components that are then used to make products. In the following example there are 3 components and 2 final products.
The demands of components and products are the following:
comp1 = 8
comp2 = 3
comp3 = 5
prod1 = 2
prod2 = 2
Which can be saved in a dictionary:
clst = {"comp": [comp1, comp2, comp3], "prod": [prod1, prod2]}
On the other hand, to manufacture a final product, the components requirements are the following:
prod1 need: 2 comp1, 1 comp2, 1 comp3
prod2 need: 3 comp1, 1 comp2, 2 comp3
In a dictionary:
rprod = {0: [2,1,1], 1: [3,1,2]}
I need to create a list that contains a list of contributions and consumptions of material that mixes the data in the following way:
[
[
[1, 1, 1, 1, 1, 1, 1, 1, -2], # ---->comp1/prod1(1)
[1, 1, 1, -1], # ---->comp2/prod1(1)
[1, 1, 1, 1, 1, -1] # ---->comp3/prod1(1)
]
,
[
[1, 1, 1, 1, 1, 1, 1, 1, -2], # ---->comp1/prod1(2)
[1, 1, 1, -1], # ---->comp2/prod1(2)
[1, 1, 1, 1, 1, -1] # ---->comp3/prod1(2)
]
,
[
[1, 1, 1, 1, 1, 1, 1, 1, -3], # ---->comp1/prod2(1)
[1, 1, 1, -1], # ---->comp2/prod2(1)
[1, 1, 1, 1, 1, -2] # ---->comp3/prod3(1)
]
,
[
[1, 1, 1, 1, 1, 1, 1, 1, -3], # ---->comp1/prod2(2)
[1, 1, 1, -1], # ---->comp2/prod2(2)
[1, 1, 1, 1, 1, -2] # ---->comp3/prod3(2)
]
]
The quantity of 1 in a list corresponds to the quantity of the demand of the component, the final number in negative is the requirement of components of the product.
Any suggestions to make the list?
Upvotes: 2
Views: 80
Reputation: 1573
Here's a readable format of the code:
rprod = {0: [2,1,1], 1: [3,1,2]}
clst = {"comp": [8, 3, 5], "prod": [2, 2]}
lst = []
for i, prod_demand in enumerate(clst["prod"]):
sub_list = []
for j, comp_demand in enumerate(clst["comp"]):
sub_list.append([1]*comp_demand+[rprod[i][j]*(-1)])
for k in range(prod_demand):
lst.append(sub_list)
But if you want to condense it, you can just write:
rprod = {0: [2,1,1], 1: [3,1,2]}
clst = {"comp": [8, 3, 5], "prod": [2, 2]}
lst = []
for i, p in enumerate(clst["prod"]):
lst += [[[1]*c+[rprod[i][j]*(-1)] for j, c in enumerate(clst["comp"])]]*p
Upvotes: 1
Reputation: 3372
In[2]: comp1 = 8
...: comp2 = 3
...: comp3 = 5
...: prod1 = 2
...: prod2 = 2
...:
...: clst = {"comp": [comp1, comp2, comp3], "prod": [prod1, prod2]}
...: rprod = {0: [2,1,1], 1: [3,1,2]}
In[3]: result = []
...: for i, p in enumerate(clst['prod']):
...: for _ in range(p):
...: tmp = [([1] * a) + [-b] for a, b in zip(clst['comp'], rprod[i])]
...: result.append(tmp)
...:
In[4]: result
Out[4]:
[[[1, 1, 1, 1, 1, 1, 1, 1, -2], [1, 1, 1, -1], [1, 1, 1, 1, 1, -1]],
[[1, 1, 1, 1, 1, 1, 1, 1, -2], [1, 1, 1, -1], [1, 1, 1, 1, 1, -1]],
[[1, 1, 1, 1, 1, 1, 1, 1, -3], [1, 1, 1, -1], [1, 1, 1, 1, 1, -2]],
[[1, 1, 1, 1, 1, 1, 1, 1, -3], [1, 1, 1, -1], [1, 1, 1, 1, 1, -2]]]
Upvotes: 2