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"
以上是我目前的代码。但是由于某种原因,它似乎正在执行第一个循环,然后执行第二个循环,然后执行第二个循环,然后执行第一个循环,然后执行第二个循环。似乎每次到达它都会执行第二个循环,然后再返回第一个for
循环。因此,我得到的答案是不正确的。
这个问题有点含糊,但是回答标题,您可以像这样同时获取键和值:
>>> d = {'a':5, 'b':6, 'c': 3}
>>> d2 = {'a':6, 'b':7, 'c': 3}
>>> for (k,v), (k2,v2) in zip(d.items(), d2.items()):
print k, v
print k2, v2
a 5
a 6
c 3
c 3
b 6
b 7
但是,请记住,字典中的键是无序的。此外,如果两个字典不包含相同数量的键,则上面的代码将失败。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句