2013-12-23 54 views
6
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循环之前,它似乎每次都进行第二个循环。正因为如此,我得到的答案是不正确的。如何一次迭代两个字典并使用两个值和两个键的结果得到结果

+1

它应该做什么?你有代码结构的方式,第二个循环在第一个循环内部,所以它会为第一个循环的每次循环执行一次第二个循环。 – user2357112

+0

请注意,字典是无序的。一次循环使用两本字典通常没有意义,除非它们具有相同的密钥。 – user2357112

+0

我需要它为第一个循环的每次迭代执行第二次循环,因为这两个字典的长度都是相同的。我需要从它们两个的信息来计算Expected_sale_value。但是由于某种原因,如果我在两个字典中都有多个键/值对,那么数学会做出奇怪的事情,并且不会总是显示出来。 – arisonu123

回答

20

的问题是有点模糊,但回答的题目,你可以在同一时间这样的键和值:

>>> 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 
+0

请勿盲目批准修改。 [这个编辑](http://stackoverflow.com/review/suggested-edits/3679345)是公然剽窃。它是从http://math.hws.edu/javanotes/TextIO_Javadoc/TextIO.html复制的。 –

+3

真的吗?我认为这是相当具有描述性的,至少暂时是如此。 – aIKid

+0

@aIKid即使两个字典的键数不一样,我们可以做到吗?我现在有这个问题... – FaCoffee

-1

看到你的问题,我建议你创建生成器表达式该导航成对两个字典,并使用max与自定义密钥来计算销售价格,以评估expected_sale_price和相应的股票

样本数据

Prices = dict(zip(range(10), ((randint(1,100), randint(1,100)) for _ in range(10)))) 
Exposure = dict(zip(range(10), ((randint(1,100), randint(1,100)) for _ in range(10)))) 

示例代码

def GetSale(Prices, Exposure): 
    '''Get Sale does not need any globals if you pass the necessary variables as 
     parameteres 
    ''' 
    from itertools import izip 
    def sale_price(args): 
     ''' 
     Custom Key, used with the max function 
     ''' 
     key, (bprice, cprice), (risk, shares) = args 
     return ((cprice - bprice) - risk * cprice) * shares 

    #Generator Function to traverse the dict in pairs 
    #Each item is of the format (key, (bprice, cprice), (risk, shares)) 
    Price_Exposure = izip(Prices.keys(), Prices.values(), Exposure.values()) 


    #Expected sale price using `max` with custom key 
    expected_sale_price = max(Price_Exposure, key = sale_price) 
    key, (bprice, cprice), (risk, shares) = expected_sale_price 
    #The best stock is the key in the expected_sale_Price 
    return "Stock {} with values bprice={}, cprice = {}, risk={} and shares={} has the highest expected sale value".format(key, bprice, cprice, risk, shares) 
3

的问题没有得到很好的定义,答案会接受一些字典失败。它依赖于按键排序,这是不能保证的。将其他键添加到词典中,删除键,甚至添加它们的顺序都会影响排序。

一个更安全的方案是选择一个字典,d在这种情况下,您可以通过组合键,然后使用这些访问第二词典:

d = {'a':5, 'b':6, 'c': 3} 
d2 = {'a':6, 'b':7, 'c': 3} 
[(k, d2[k], v) for k, v in d.items()] 

结果:

[('b', 7, 6), ('a', 6, 5), ('c', 3, 3)] 

这并不比其他答案复杂,并且明确了哪些密钥正在被访问。如果字典具有不同的密钥排序,例如d2 = {'x': 3, 'b':7, 'c': 3, 'a':9},则仍会给出一致的结果。