2013-04-05 76 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),这可能会做你正在寻找的东西。