如何使用一个字典作为另一个字典的键值的变量。

huangapple go评论99阅读模式
英文:

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&#39;m writing a pseudo supply and demand-based Price board for my Python final and I&#39;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 = {
  &#39;Iron Ingot&#39;: 1,
  &#39;Copper Ingot&#39;:10,
  &#39;Tin Ingot&#39;:30,
  &#39;Silver Ingot&#39;:100,
  &#39;Gold Ingot&#39;:1000,
  &#39;Mithral Ingot&#39;: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 = {
  &quot;metals&quot; : [&#39;Iron Ingots&#39;, &#39;Copper Ingots&#39;, &#39;Tin Ingots&#39;, &#39;Silver Ingots&#39;, &#39;Gold Ingots&#39;, &#39;mithral Ingots&#39;],
  &quot;wholesale&quot; : [metalDict[&#39;Iron Ingot&#39;], metalDict[&#39;Copper Ingot&#39;],metalDict[&#39;Tin Ingot&#39;],metalDict[&#39;Silver Ingot&#39;], metalDict[&#39;Gold Ingot&#39;], metalDict[&#39;Mithral Ingot&#39;]],
  &#39;city 1&#39; : [1,2,3,4,5,6],
  &#39;city 2&#39; : [1,2,3,4,5,6],
  &#39;city 3&#39; : [1,2,3,4,5,6],
  &#39;city 4&#39; : [1,2,3,4,5,6],
  &#39;city 5&#39; : [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 &#39;city 1&#39; (I have cut out my demand code for the sake of forcing the change to happen regardless of my &#39;city 1&#39; needs).

city1Needs = np.random.randint (2, size=100)
city1Demand = 40
if city1Demand &lt;= 40:
  for key, value in outputDict.items():
    if key == &#39;city 1&#39;:
      Outputdict[&#39;city 1&#39;] = (metalDict[key]/2) + metalDict[key]

What I would expect to happen is it takes each of the row indices of &#39;city 1&#39; 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 = [&#39;Iron Ingot&#39;, &#39;Copper Ingot&#39;, &#39;Tin Ingot&#39;, &#39;Silver Ingot&#39;, &#39;Gold Ingot&#39;, &#39;Mithral Ingot&#39;]

cities = [&#39;city 1&#39;,&#39;city 2&#39;,&#39;city 3&#39;,&#39;city 4&#39;,&#39;city 5&#39;]

wholesale = {
  &#39;Iron Ingot&#39;: 1,
  &#39;Copper Ingot&#39;:10,
  &#39;Tin Ingot&#39;:30,
  &#39;Silver Ingot&#39;:100,
  &#39;Gold Ingot&#39;:1000,
  &#39;Mithral Ingot&#39;: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:

{&#39;city 1&#39;: [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
 &#39;city 2&#39;: [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
 &#39;city 3&#39;: [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
 &#39;city 4&#39;: [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0],
 &#39;city 5&#39;: [1.5, 15.0, 45.0, 150.0, 1500.0, 7500.0]}

huangapple
  • 本文由 发表于 2023年8月5日 02:20:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定