2013-04-05 73 views
0

我對編程語言還不是很熟悉,我很努力去理解返回值的low_price和返回(顯然)對象的high_price之間的區別。有人能解釋我爲什麼他們有不同的結構嗎? 兩者都是字典。 high_price正在通過一項功能。希望它是有道理的。道歉,如果縮進是不正確的,我仍然努力得到這個權利與網站也!python字典對象vs值

Output 
{0: {...}} 
({0: {...}}, 99.9969999999999) 

def agregate_freq(freq, high_price,low_price):  
    if mynumber >high_price[0]: #new one 
     high_price[0] = mynumber 
    #if mynumber <low_price[0]: #new one 
    # low_price[0] = mynumber 
    print(high_price[0]) 
    return (high_price) 

    if mynumber <low_price[0]: #new one 
     low_price[0] = mynumber 

    high_price[0] = agregate_freq(0,high_price,low_price) 
    print (high_price[0],low_price[0]) 

回答

1

return (high_price)只是返回按預期的方式表達high_price的值和的元組。 (high_price[0],low_price[0])是一個元組。如果你想要一個元素元組,請在你的high_price之後加一個逗號;像return (high_price,)

這就是爲什麼看到的輸出有差異的原因。

1

當您做high_price[0] = agregate_freq(0,high_price,low_price)時,您將high_price的第0個元素設置爲您的agregate_freq函數的返回值。該函數本身返回high_price,所以很奇怪,我認爲你將high_price的第一個元素設置爲high_price本身。你沒有這樣做到low_price
我只是猜測,但你的功能似乎是做任務本身,所以你可能不希望它返回任何東西。請自行撥打agregate_freq(0,high_price,low_price),這可能會做你正在尋找的東西。