python无法返回变量但可以打印 [英] python can't return a variable but can print

查看:74
本文介绍了python无法返回变量但可以打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

def findFourPlus(itemCount, seq,goal):goalDifference = float("inf")最近部分 = []你好=subset_sum(itemCount,seq,goal,goalDifference,closestPartial,partial=[])return hello #不从subset_sum()返回值def subset_sum(itemCount, seq,goal,goalDifference,closestPartial,partial):s = 总和(部分)# 检查部分和是否等于目标if(len(partial) == itemCount):如果 s == 目标:打印(找到 YAA")return partial #right 现在不返回任何内容.我打算让它在找到一对解决方案后立即跳出此功能.别的:if( abs(goal - s) 

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

<预><代码>>>>findFourPlus(3, [1,2,3,4,5,6,7,8,9,10], 20)找到亚亚[1, 9, 10]找到亚亚[2, 8, 10]找到亚亚[3, 7, 10]找到亚亚[3, 8, 9]找到亚亚[4, 6, 10]找到亚亚[4, 7, 9]找到亚亚[5, 6, 9]找到亚亚[5, 7, 8]

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

<预><代码>>>>findFourPlus(3, [1,2,3,4,5,6,7,8,9,10], 20)找到亚亚找到亚亚找到亚亚找到亚亚找到亚亚找到亚亚找到亚亚找到亚亚

在编写 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)//如果我调用 answer,它会返回给我 [1,4]

示例:

<预><代码>>>>a = findFourPlus(2, [1,2,3,4,5], 6) # 打印出所有内容,这对测试用例很有用[1, 2][1, 3][1, 4]找到亚亚[1, 5]没有任何[2, 3]找到亚亚[2, 4][2, 5]没有任何[3, 4][3, 5]没有任何[4, 5]没有任何没有任何>>># 调用时不返回任何内容>>>

打印只是为了确保我的逻辑是正确的.抱歉造成混乱,但希望这能澄清它!

解决方案

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

如果在递归调用的循环中添加 return 语句,如果子调用找到了一些东西,你就会得到你正在搜索的东西.

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

def findFourPlus(itemCount, seq,goal):goalDifference = float("inf")最近部分 = []你好=subset_sum(itemCount,seq,goal,0,closestPartial,partial=[])return hello #不从subset_sum()返回值def subset_sum(itemCount, seq,goal,goalDifference,closestPartial,partial):s = 总和(部分)# 检查部分和是否等于目标if(len(partial) == itemCount):如果 s == 目标:打印(找到 YAA")return partial #right 现在不返回任何内容.我打算让它在找到一对解决方案后立即跳出此功能.别的:if( abs(goal - s) <=goalDifference):#print(abs(goal-s),goalDifference,closestPartial)目标差异 = abs(目标 - s)最近部分[:] = 部分#print(abs(goal-s),goalDifference,closestPartial)返回最近部分对于范围内的 i(len(seq)):n = seq[i]剩余 = seq[i+1:]t = subset_sum(itemCount, 剩余, 目标,goalDifference,closestPartial,partial + [n])打印 t如果:返回a = findFourPlus(2, [1,2,3,4,5], 6)打印a=",一个

输出:

无没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何没有任何找到亚亚[1, 5][1, 5]a= [1, 5]

So I'm encountering a weird problem.

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

In the subset_sum() function, I can print out the partial variable, and it will return to me the right value

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

However, if i change the print(partial) line to return partial, it will still print out the FOUNDYAA line since it is printed, but it will not return the 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

I wrote other functions before I wrote findFourPlus and subset_sum, and those worked fine when I attempt to return something. What am i doing wrong here?

Edit: I seem to be confusing the readers here. What I am ultimately trying to do is to store the sets of integeres (the list ) in a variable by calling the findFourPlus() function. Say I want to see if, in the list [1,2,3,4,5], would any two of them add up equal 5, I would call findFourPlus(2, [1,2,3,4,5], 5), and it would hopefully return to me [1, 4]. With that, I can store the answer [1,4] into a variable, say answer, by calling

answer = findFourPlus(2, [1,2,3,4,5], 5) // if I call answer, it'll return to me [1,4]

An example:

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

The printing are all just to make sure my logic is correct. Sorry for the confusion, but hopefully this clarifies it!

解决方案

You are doing recursive calls to subset_sum, but you are not passing the returned value from the subcall to the caller.

If you add a return statement in the loop for recursive calls, you get what you are searching for, if the subcall found something.

I don't see the need for the closestPartial, because if you set it to inf, the function returns the first sequence that contains itemcount elements. I've also changed the test to <= to work with 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

Output :

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

这篇关于python无法返回变量但可以打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆