英文:
How Would I Use One Dictionary to be a Variable for Another Dictionary's Key Values
问题
I'll provide the translated code section for you:
所以,我正在编写一个基于供需的伪价格面板,用于我的Python期末项目,我遇到了一个问题,即如何使用一个字典来影响另一个字典的值。首先,我有一个包含各种金属的字典,其中包含我设定的基础成本。
```python
metalDict = {
'铁锭': 1,
'铜锭': 10,
'锡锭': 30,
'银锭': 100,
'金锭': 1000,
'秘银锭': 5000}
然后,我有一个次要的字典,根据各种背景计算来显示所有信息,并从metalDict
中导入批发成本。以下是我目前的代码:
outputDict = {
"金属": ['铁锭', '铜锭', '锡锭', '银锭', '金锭', '秘银锭'],
"批发价": [metalDict['铁锭'], metalDict['铜锭'], metalDict['锡锭'], metalDict['银锭'], metalDict['金锭'], metalDict['秘银锭']],
'城市1': [1, 2, 3, 4, 5, 6],
'城市2': [1, 2, 3, 4, 5, 6],
'城市3': [1, 2, 3, 4, 5, 6],
'城市4': [1, 2, 3, 4, 5, 6],
'城市5': [1, 2, 3, 4, 5, 6]}
我想要使用metalDict
的基础成本(根据我编写的非常基本的生产算法而变化)作为更新城市1 - 5中的成本的方程的一部分。这可能不可行,或者我可能需要以另一种方式解决这个问题(也许为每个城市创建一个字典?);因为我对Python还相对新手,所以不太确定。
另外,我在输出时使用了Numpy和Pandas,所以也许它们有我尚不了解的功能。
我已经尝试通过for
循环迭代值更改,但是我遇到了一个键错误'城市1'
(出于强制更改的目的,我删除了我的需求代码,而不考虑'城市1'
的需求)。
city1Needs = np.random.randint(2, size=100)
city1Demand = 40
if city1Demand <= 40:
for key, value in outputDict.items():
if key == '城市1':
outputDict['城市1'] = (metalDict[key] / 2) + metalDict[key]
我期望发生的是它接受'城市1'
的每个行索引,并执行与metalDict
的行中的每个值相对应的数学运算。所以,我的表格看起来会像这样(我无法弄清楚如何在编辑器中扩展表格,所以这只是2行和列而不是整个表格):
批发价格 | 城市一 |
---|---|
1 | 1.5 |
10 | 15 |
请让我知道,如果您需要进一步的帮助或解释。
<details>
<summary>英文:</summary>
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.
```python
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 |
答案1
得分: 0
我想鼓励你考虑其他组织这些数据的方式。不清楚你想要的最终产品是什么,但请查看我在这里所做的。通过将金属列表和城市列表分开,可以让我逐列或逐行处理数据,而不是专注于单个数字。
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)
输出:
{'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]}
英文:
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]}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论