Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,182,793 members, 7,918,554 topics. Date: Monday, 12 August 2024 at 11:46 AM

## Python: I Have A Dictionary Problem ;) :-[ - Programming - Nairaland

 Python: I Have A Dictionary Problem ;) :-[ by dozies1: 7:34am On Sep 19, 2015 Found this code to solve transportation problem using Vogel's approximation. My goal is to modify it to accept input from a user but, my understanding of dictionary in python is quite weak. Hence any suggestion would be appreciated.`from collections import defaultdict costs = {'W': {'A': 16, 'B': 16, 'C': 13, 'D': 22, 'E': 17}, 'X': {'A': 14, 'B': 14, 'C': 13, 'D': 19, 'E': 15}, 'Y': {'A': 19, 'B': 19, 'C': 20, 'D': 23, 'E': 50}, 'Z': {'A': 50, 'B': 12, 'C': 50, 'D': 15, 'E': 11}}demand = {'A': 30, 'B': 20, 'C': 70, 'D': 30, 'E': 60}cols = sorted(demand.iterkeys())supply = {'W': 50, 'X': 60, 'Y': 50, 'Z': 50}res = dict((k, defaultdict(int)) for k in costs)g = {}for x in supply: g[x] = sorted(costs[x].iterkeys(), key=lambda g: costs[x][g])for x in demand: g[x] = sorted(costs.iterkeys(), key=lambda g: costs[g][x]) while g: d = {} for x in demand: d[x] = (costs[g[x][1]][x] - costs[g[x][0]][x]) if len(g[x]) > 1 else costs[g[x][0]][x] s = {} for x in supply: s[x] = (costs[x][g[x][1]] - costs[x][g[x][0]]) if len(g[x]) > 1 else costs[x][g[x][0]] f = max(d, key=lambda n: d[n]) t = max(s, key=lambda n: s[n]) t, f = (f, g[f][0]) if d[f] > s[t] else (g[t][0], t) v = min(supply[f], demand[t]) res[f][t] += v demand[t] -= v if demand[t] == 0: for k, n in supply.iteritems(): if n != 0: g[k].remove(t) del g[t] del demand[t] supply[f] -= v if supply[f] == 0: for k, n in demand.iteritems(): if n != 0: g[k].remove(f) del g[f] del supply[f] for n in cols: print "\t", n,printcost = 0for g in sorted(costs): print g, "\t", for n in cols: y = res[g][n] if y != 0: print y, cost += y * costs[g][n] print "\t", printprint "\n\nTotal Cost = ", cost`[url]http://rosettacode.org/wiki/Vogel's_approximation_method#Python[/url] Re: Python: I Have A Dictionary Problem ;) :-[ by Eminya(f): 7:38am On Sep 19, 2015 Didn't understand a thing, walks out of thread confused 1 Like Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 7:55am On Sep 19, 2015 http://rosettacode.org/wiki/Vogel's_approximation_method#Python Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 7:56am On Sep 19, 2015 Eminya: Didn't understand a thing, walks out of thread confusedWhat's so confusing? Re: Python: I Have A Dictionary Problem ;) :-[ by prowlerovervoid: 8:32am On Sep 19, 2015 Eminya: Didn't understand a thing, walks out of thread confusedsame same. Re: Python: I Have A Dictionary Problem ;) :-[ by lordZOUGA(m): 8:35am On Sep 19, 2015 dozies1, what is your point? do you go about posting un-commented source codes with non-existent titles?What does the code do and whwt problem do you have with it? Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 8:40am On Sep 19, 2015 Sorry about that. I mistakenly deleted my question while modifying my postlordZOUGA:dozies1, what is your point? do you go about posting un-commented source codes with non-existent titles?What does the code do and whwt problem do you have with it? Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 8:49am On Sep 19, 2015 sorry about thatprowlerovervoid:same same. Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 8:58am On Sep 19, 2015 basically i want turn the keys and values of this:`costs = {'W': {'A': 16, 'B': 16, 'C': 13, 'D': 22, 'E': 17}, 'X': {'A': 14, 'B': 14, 'C': 13, 'D': 19, 'E': 15}, 'Y': {'A': 19, 'B': 19, 'C': 20, 'D': 23, 'E': 50}, 'Z': {'A': 50, 'B': 12, 'C': 50, 'D': 15, 'E': 11}}demand = {'A': 30, 'B': 20, 'C': 70, 'D': 30, 'E': 60}supply = {'W': 50, 'X': 60, 'Y': 50, 'Z': 50}`to user input Re: Python: I Have A Dictionary Problem ;) :-[ by prowlerovervoid: 9:51am On Sep 19, 2015 dozies1:basically i want turn the keys and values of this:to user inputuser input? how? to what function? Re: Python: I Have A Dictionary Problem ;) :-[ by KelvinMega(m): 10:12am On Sep 19, 2015 use the raw_input function and assign it to a variable then use the variable as the key to your dictionary as inMyKey = raw_input('enter your key')MyDict = {MyKeywhatever you want here)}you should give a good description of what you're trying to achieve if you don't mind. there should be a better way out rather than this Re: Python: I Have A Dictionary Problem ;) :-[ by lordZOUGA(m): 10:19am On Sep 19, 2015 dozies1:basically i want turn the keys and values of this:to user inputDoes the user already know the value of the keys? for example does the user already know that 'W' matches this: {'A': 16, 'B': 16, 'C': 13, 'D': 22, 'E': 17}?if not you probably should show them the table once they start the program.if they do, you just have to accept whatever input they provide verify it and match it to the table and then compute.for example:`#based on python3print("Please input a cost: "cost = input()if cost and type(cost) is str and cost in costs.keys(): #do stuff with cost possible retrieve it's value from the dictionary print(costs[cost])` Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 10:41am On Sep 19, 2015 Thanks for your contribution KelvinMega:use the raw_input function and assign it to a variable then use the variable as the key to your dictionary as inMyKey = raw_input('enter your key')MyDict = {MyKeywhatever you want here)}you should give a good description of what you're trying to achieve if you don't mind. there should be a better way out rather than thisI'm trying to write a program that solves transportation problem this way Vogel's Approximation Method of Allocation.This method also takes costs into account inallocation. Five steps are involved in applyingthis heuristic:Step 1: Determine the difference between thelowest two cells in all rows and columns,including dummies.Step 2: Identify the row or column with thelargest difference. Ties may be broken arbitrarily.Step 3: Allocate as much as possible to thelowest-cost cell in the row or column with thehighest difference. If two or more differencesare equal, allocate as much as possible to thelowest-cost cell in these rows or columns.Step 4: Stop the process if all row and columnrequirements are met. If not, go to the nextstep.Step 5: Recalculate the differences between thetwo lowest cells remaining in all rows andcolumns. Any row and column with zero supplyor demand should not be used in calculatingfurther differences. Then go to Step 2. http://orms.pef.czu.cz/text/VogelAproxim.html Re: Python: I Have A Dictionary Problem ;) :-[ by dozies1: 11:06am On Sep 19, 2015 lordZOUGA:Does the user already know the value of the keys? for example does the user already know that 'W' matches this: {'A': 16, 'B': 16, 'C': 13, 'D': 22, 'E': 17}?if not you probably should show them the table once they start the program.if they do, you just have to accept whatever input they provide verify it and match it to the table and then compute.for example:`#based on python3print("Please input a cost: "cost = input()if cost and type(cost) is str and cost in costs.keys(): #do stuff with cost possible retrieve it's value from the dictionary print(costs[cost])`Thanks. The user wouldn't know. I would try your suggestion.