相关疑难解决方法(0)

如何一次迭代两个字典并使用两者中的值和键获得结果

def GetSale():#calculates expected sale value and returns info on the stock with              highest expected sale value
      global Prices
      global Exposure
      global cprice
      global bprice
      global risk
      global shares
      global current_highest_sale
      best_stock=' '
      for value in Prices.values():
          cprice=value[1]
          bprice=value[0]
          for keys, values in Exposure.items():
             risk=values[0]
             shares=values[1]
             Expected_sale_value=( (cprice - bprice ) - risk * cprice) * shares
             print (Expected_sale_value)
             if current_highest_sale < Expected_sale_value:
                current_highest_sale=Expected_sale_value
                best_stock=Exposure[keys]
     return best_stock +" has the highest expected sale value"
Run Code Online (Sandbox Code Playgroud)

以上是我目前的代码.但由于某种原因,它似乎是在做第一个循环,然后是第二个循环,然后是第二个循环,然后是第一个循环,然后是第二个循环.在返回第一个for循环之前,它似乎每次进入第二个循环.正因为如此,我得到的答案是不正确的.

python loops

14
推荐指数
2
解决办法
3万
查看次数

标签 统计

loops ×1

python ×1