So I’m writing a pseudo supply and demand-based Price board for my Python final and I’m having an issue with using one dictionary to influence the values of another. To start, I have a dictionary of various metals I have base costs for.
metalDict = {
'Iron Ingot': 1,
'Copper Ingot':10,
'Tin Ingot':30,
'Silver Ingot':100,
'Gold Ingot':1000,
'Mithral Ingot':5000}
Then I have a secondary dictionary that will display all information according to a variety of background calculations and import the wholesale cost from metalDict. This is what my code currently looks like:
outputDict = {
"metals" : ['Iron Ingots', 'Copper Ingots', 'Tin Ingots', 'Silver Ingots', 'Gold Ingots', 'mithral Ingots'],
"wholesale" : [metalDict['Iron Ingot'], metalDict['Copper Ingot'],metalDict['Tin Ingot'],metalDict['Silver Ingot'], metalDict['Gold Ingot'], metalDict['Mithral Ingot']],
'city 1' : [1,2,3,4,5,6],
'city 2' : [1,2,3,4,5,6],
'city 3' : [1,2,3,4,5,6],
'city 4' : [1,2,3,4,5,6],
'city 5' : [1,2,3,4,5,6]}
I want to use the metalDict base costs (which change based on a very basic production algorithm I wrote) to be part of an equation that updates the costs in cities 1 – 5. This may not be possible or I may need to tackle this another way (maybe creating a dictionary for each city?); I’m not sure since I’m fairly new to Python.
Also, I’m using Numpy and Pandas for outputs, so maybe they have functionality I’m not aware of yet.
What I have done is try and iterate the value changes through a for loop but I’m getting a key error 'city 1' (I have cut out my demand code for the sake of forcing the change to happen regardless of my 'city 1' needs).
city1Needs = np.random.randint (2, size=100)
city1Demand = 40
if city1Demand <= 40:
for key, value in outputDict.items():
if key == 'city 1':
Outputdict['city 1'] = (metalDict[key]/2) + metalDict[key]
What I would expect to happen is it takes each of the row indices of 'city 1' and to the math corresponding to each of the values in metalDict‘s rows. So my table would look something like this (I could not figure out how to expand the table in the editor so this would just be 2 of the rows and columns as opposed to the entire table):
| Wholesle Price | City one |
|---|---|
| 1 | 1.5 |
| 10 | 15 |
>Solution :
I want to encourage you to think about other ways to organize this data. It’s not clear what you want your final product to be, but look over what I’ve done here. By keeping the list of metals and the list of cities separate, it lets me work with the data a column at a time, or a row at a time, instead of focusing on individual numbers.
metals = ['Iron Ingot', 'Copper Ingot', 'Tin Ingot', 'Silver Ingot', 'Gold Ingot', 'Mithral Ingot']
cities = ['city 1','city 2','city 3','city 4','city 5']
wholesale = {
'Iron Ingot': 1,
'Copper Ingot':10,
'Tin Ingot':30,
'Silver Ingot':100,
'Gold Ingot':1000,
'Mithral Ingot':5000}
outputDict = {}
for city in cities:
outputDict[city] = []
for metal in metals:
outputDict[city].append( wholesale[metal] * 1.5 )
from pprint import pprint
pprint(outputDict)
Output:
{'city 1': [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
'city 2': [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
'city 3': [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
'city 4': [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
'city 5': [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0]}