'How do I use dictionary keys as an index in a loop and as a value?
I'm in Python and trying to produce an output from cycling through a dictionary created after running an algorithm. The dictionary represents unmet demand for network k, node i, product g, at time t.
I have a loop that indexes correctly and shows me the sum total across network k, node i, and product g.
timep = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# dnode is a tuplelist with three keys: (k,i,g)
objvaluetime = {}
for t in timep:
objvaluetime[t] = sum(mydict_weightn[(k, i,g)] * (mydict_phite[(k, i,g)] - solutiondnode[k, i, g, t]) for k,i,g in dnode)/ sum(mydict_weightn[(k,i,g)] * mydict_phite[(k, i,g)] for k, i,g in dnode)
Output:
{1: 0.05890098869573218,
2: 0.05890098869573218,
3: 0.020467694724039757,
4: 0.020467694724039757,
5: 0.020467694724039757,
6: 0.020467694724039757,
7: 0.020467694724039757,
8: 0.020467694724039757,
9: 0.020467694724039757,
10: 0.020467694724039757,
11: 0.020467694724039757,
12: 0.020467694724039757,
13: 0.020467694724039757,
14: 0.020467694724039757,
15: 0.020467694724039757,
16: 0.020467694724039757,
17: 0.020467694724039757,
18: 0.020467694724039757,
19: 0.020467694724039757,
20: 0.020467694724039757}
Now I'd like to do something similar, except further breaking down the unmet demand at time t = 1-20 by product g (which takes on a value 1-11).
I'd like the output to represent the values for (1,1), (1,2), (1,3) . . . (1,11), (2,1), (2,2) . . .(20,11)
This is my loop so far, but it is not correctly indexing by product g, it is simply showing the same value for all g which is incorrect.
objvaluetimecommodity = {}
for t in timep:
for k,i,g in dnode:
objvaluetimecommodity[t,g] = sum(mydict_weightn[(k, i,g)] * (mydict_phite[(k, i,g)] - solutiondnode[k, i, g, t]) for k,i,g in dnode)/ sum(mydict_weightn[(k,i,g)] * mydict_phite[(k, i,g)] for k, i,g in dnode)
Output:
{(1, 1): 0.05890098869573218,
(1, 2): 0.05890098869573218,
(1, 3): 0.05890098869573218,
(1, 4): 0.05890098869573218,
(1, 5): 0.05890098869573218,
(1, 6): 0.05890098869573218,
(1, 7): 0.05890098869573218,
(1, 8): 0.05890098869573218,
(1, 9): 0.05890098869573218,
(1, 10): 0.05890098869573218,
(1, 11): 0.05890098869573218,
(2, 1): 0.05890098869573218 . . .}
Edit: Essentially, I want to do what was done in objvaluetime and break it down further by product g. The values in objvaluetime represent unmet demand at a particular time. I want to see unmet demand by product (g), which happens to be one of the keys, so the sum expression may need to be modified.
Solution 1:[1]
(Answer for clarity, not a real "answer")
Your variables k,i,g in your for loop:
for k,i,g in dnode:
are being shadowed by your generator expression:
sum(
mydict_weightn[(k,i,g)] * (
mydict_phite[(k,i,g)] - solutiondnode[k, i, g, t]
) for k,i,g in dnode # <- k,i,g used again here
)
It's really hard to tell from your description how you should solve this.
If you want to clarify what you want we might be able to help more.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jedwards |
