python不能返回变量但可以打印

时间:2014-09-22 17:51:50

标签: python variables return

所以我遇到了一个奇怪的问题。

def findFourPlus(itemCount, seq, goal):
    goalDifference = float("inf")
    closestPartial = []
    hello = subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial=[])
    return hello #doesn't return value from subset_sum()

def subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial):
    s = sum(partial)

    # check if the partial sum is equals to target
    if(len(partial) == itemCount):
        if s == goal:
                print("FOUND YAA")
                return partial #right now doesn't return anything. I intend for it to break out of this function as soon as it finds one pair of solution. 


        else:
            if( abs(goal - s) < goalDifference):
                #print(abs(goal-s), goalDifference, closestPartial)
                goalDifference = abs(goal - s)
                closestPartial[:] = partial
                #print(abs(goal-s), goalDifference, closestPartial)
                return closestPartial

    for i in range(len(seq)):
        n = seq[i]
        remaining = seq[i+1:]
        print(subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n]))

在subset_sum()函数中,我可以打印出部分变量,它会返回给我正确的值

>>> findFourPlus(3, [1,2,3,4,5,6,7,8,9,10], 20)
FOUND YAA
[1, 9, 10]
FOUND YAA
[2, 8, 10]
FOUND YAA
[3, 7, 10]
FOUND YAA
[3, 8, 9]
FOUND YAA
[4, 6, 10]
FOUND YAA
[4, 7, 9]
FOUND YAA
[5, 6, 9]
FOUND YAA
[5, 7, 8]

但是,如果我将打印(部分)行更改为返回部分,它仍将打印出FOUNDYAA行,因为它已打印,但不会返回部分

>>> findFourPlus(3, [1,2,3,4,5,6,7,8,9,10], 20)
FOUND YAA
FOUND YAA
FOUND YAA
FOUND YAA
FOUND YAA
FOUND YAA
FOUND YAA
FOUND YAA

我在编写findFourPlus和subset_sum之前编写了其他函数,当我尝试返回某些东西时,这些函数运行正常。我在这做错了什么?

编辑:我似乎在这里混淆了读者。我最终要做的是通过调用findFourPlus()函数将一组integeres(列表)存储在一个变量中。假设我想看看,在列表[1,2,3,4,5]中,如果其中任何两个加起来等于5,我会调用 findFourPlus(2,[1,2,3,4] ,5),5),希望能回到我身边[1,4]。有了这个,我可以通过调用

将答案[1,4]存储到变量中,比如回答
answer = findFourPlus(2, [1,2,3,4,5], 5) // if I call answer, it'll return to me [1,4]

一个例子:

>>> a = findFourPlus(2, [1,2,3,4,5], 6) # prints out everything, which is good for test cases
[1, 2]
[1, 3]
[1, 4]
FOUND YAA
[1, 5]
None
[2, 3]
FOUND YAA
[2, 4]
[2, 5]
None
[3, 4]
[3, 5]
None
[4, 5]
None
None
>>> a # doesn't return anything when called
>>> 

打印只是为了确保我的逻辑是正确的。很抱歉有这种困惑,但希望这可以澄清它!

4 个答案:

答案 0 :(得分:3)

执行函数时,它不会自动将其返回值转储到输出中,因此在for循环中,

for i in range(len(seq)):
    n = seq[i]
    remaining = seq[i+1:]
    print(subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n]))

答案 1 :(得分:2)

您正在对subset_sum进行递归调用,但是您没有将返回的值从子查询传递给调用者。

如果在循环中为递归调用添加return语句,则会获得您要搜索的内容,如果子查找了一些内容。

我认为不需要closestPartial,因为如果将其设置为inf,该函数将返回包含itemcount元素的第一个序列。我还将测试更改为<=以使用0。

def findFourPlus(itemCount, seq, goal):
    goalDifference = float("inf")
    closestPartial = []
    hello = subset_sum(itemCount, seq, goal, 0, closestPartial, partial=[])
    return hello #doesn't return value from subset_sum()

def subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial):
    s = sum(partial)

    # check if the partial sum is equals to target
    if(len(partial) == itemCount):
        if s == goal:
                print("FOUND YAA")
                return partial #right now doesn't return anything. I intend for it to break out of this function as soon as it finds one pair of solution. 


        else:
            if( abs(goal - s) <= goalDifference):
                #print(abs(goal-s), goalDifference, closestPartial)
                goalDifference = abs(goal - s)
                closestPartial[:] = partial
                #print(abs(goal-s), goalDifference, closestPartial)
                return closestPartial

    for i in range(len(seq)):
        n = seq[i]
        remaining = seq[i+1:]
        t = subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n])
        print t
        if t:
            return t

a = findFourPlus(2, [1,2,3,4,5], 6)
print "a=", a

输出:

None
None
None
None
None
None
None
None
None
None
None
None
None
None
FOUND YAA
[1, 5]
[1, 5]
a= [1, 5]

答案 2 :(得分:1)

subset_sum的某些执行路径没有return语句,因此将返回None(并由findFourPlus转发)。 在解释器中,评估None不会打印任何内容:

>>> def f():
...  pass
...
>>> a = f()
>>> a is None
True
>>> a
>>>

如果您想确保看到某些内容,请尝试:

>>> str(a)
'None'

答案 3 :(得分:0)

def subset_sum(itemCount, seq, goal, goalDifference, closestPartial, partial):

    s = sum(partial)

    # check if the partial sum is equals to target
    if(len(partial) == itemCount):
        if s == goal:
                print("FOUND YAA")
                print(partial)
                return partial
        else:
            if( abs(goal - s) < goalDifference):
                #print(abs(goal-s), goalDifference, closestPartial)
                goalDifference = abs(goal - s)
                closestPartial[:] = partial
                #print(abs(goal-s), goalDifference, closestPartial)

for i in range(len(seq)):
    n = seq[i]
    remaining = seq[i+1:]
    result = subset_sum(itemCount, remaining, goal, goalDifference, closestPartial, partial + [n])
    if result:    ## If you just want the
        break     ## first one
else:
    print ("Nothing found")