Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,630 members, 7,809,348 topics. Date: Friday, 26 April 2024 at 08:09 AM

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

Nairaland Forum / Science/Technology / Programming / Python: I Have A Dictionary Problem ;) :-[ (1494 Views)

How Can I Create An Offline Dictionary App For Android / Add A Dictionary To A Grid / English Dictionary In Xml/json Format (2) (3) (4)

(1) (Reply) (Go Down)

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,
print
cost = 0
for 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",
print
print "\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
undecided 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:
undecided Didn't understand a thing, walks out of thread confused
What's so confusing?
Re: Python: I Have A Dictionary Problem ;) :-[ by prowlerovervoid: 8:32am On Sep 19, 2015
Eminya:
undecided Didn't understand a thing, walks out of thread confused
same 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 post
lordZOUGA:
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 that
prowlerovervoid:

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 input
user 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 in
MyKey = raw_input('enter your key')
MyDict = {MyKeysadwhatever 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 input

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 python3

print("Please input a cost: "wink
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 in
MyKey = raw_input('enter your key')
MyDict = {MyKeysadwhatever 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
I'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 in
allocation. Five steps are involved in applying
this heuristic:
Step 1: Determine the difference between the
lowest two cells in all rows and columns,
including dummies.
Step 2: Identify the row or column with the
largest difference. Ties may be broken arbitrarily.
Step 3: Allocate as much as possible to the
lowest-cost cell in the row or column with the
highest difference. If two or more differences
are equal, allocate as much as possible to the
lowest-cost cell in these rows or columns.
Step 4: Stop the process if all row and column
requirements are met. If not, go to the next
step.
Step 5: Recalculate the differences between the
two lowest cells remaining in all rows and
columns. Any row and column with zero supply
or demand should not be used in calculating
further 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 python3

print("Please input a cost: "wink
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.

(1) (Reply)

Job Opportunity / Please Web Programmers Help Me On This / Application In Php

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.