python不能返回变量,但可以prin

2024-10-01 15:34:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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()函数中,我可以打印出部分变量,它将返回正确的值

^{pr2}$

但是,如果我将print(partial)行改为return partial,它仍然会打印出FOUNDYAA行,但不会返回partial

>>> 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()函数将整数集(列表)存储在变量中。假设我想知道,在列表[1,2,3,4,5]中,是否有任何两个加起来等于5,我会调用findFourPlus(2,[1,2,3,4,5],5),它很可能会返回给我[1,4]。这样,我就可以通过调用将答案[1,4]存储到一个变量中,比如answer

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

打印只是为了确保我的逻辑是正确的。很抱歉让你困惑,但希望这能澄清它!在


Tags: nonereturnifabspartialseqsumprint
3条回答

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

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

如果您想确定看到什么,请尝试:

^{pr2}$

您正在执行对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

输出:

^{pr2}$

当您执行一个函数时,它不会自动将返回值转储到输出中,所以在for循环中

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

相关问题 更多 >

    热门问题