二和,从内部传递值到函数调用?

2024-09-30 14:25:41 发布

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

我尝试将满足目标的唯一对传递给函数调用。python有点新,所以请告诉我如何修复它

array = [ 3, 4, 5, 9, 10, -1, 6 ]
target = 9
def twoSum (array, target):
    for i in range(0, len(array)):
        for x in range( i + 1, len(array)):
            totalOfTwo = array[i] + array[x]
            if (totalOfTwo == target):
                pairsList = (array[i], array[x])
    return -1
result = twoSum (array, target)

if result != -1:
    print ("the intergers numbers meet target", result)
else:
    print ("result is not in range")

Tags: intarget目标forlenifdefrange
3条回答

我想这就是你要找的

您需要将这些对添加到列表中,如果找到,则返回它们

另外,就我个人而言,如果没有找到空列表,我会返回空列表,而不是-1,因为它们是不同的数据类型

array = [ 3, 4, 5, 9, 10, -1, 6 ]
target = 9
def twoSum (array, target):
    pairsList = []
    for i in range(0, len(array)):
        for x in range( i + 1, len(array)):
            totalOfTwo = array[i] + array[x]
            if (totalOfTwo == target):
                pairsList.append((array[i], array[x]))
    if len(pairsList) == 0:
        return -1
    else:
        return pairsList
result = twoSum (array, target)

if result != -1:
    print ("the intergers numbers meet target", result)
else:
    print ("result is not in range")

您忘记返回结果

你的密码

array = [ 3, 4, 5, 9, 10, -1, 6 ]
target = 9
def twoSum (array, target):
    for i in range(0, len(array)):
        for x in range( i + 1, len(array)):
            totalOfTwo = array[i] + array[x]
            if (totalOfTwo == target):
                pairsList = (array[i], array[x]) ##### THIS #####
    return -1
result = twoSum (array, target)

if result != -1:
    print ("the intergers numbers meet target", result)
else:
    print ("result is not in range")

我的代码

array = [ 3, 4, 5, 9, 10, -1, 6 ]
target = 9
def twoSum (array, target):
    for i in range(0, len(array)):
        for x in range( i + 1, len(array)):
            totalOfTwo = array[i] + array[x]
            if (totalOfTwo == target):
               return (array[i], array[x]) ##### THIS ####
    return -1
result = twoSum (array, target)

if result != -1:
    print ("the intergers numbers meet target", result)
else:
    print ("result is not in range")

但这只是第一个结果,所以

array = [ 3, 4, 5, 9, 10, -1, 6 ]
target = 9
def twoSum (array, target):
    rsts = [] # save rsts hear
    for i in range(0, len(array)):
        for x in range( i + 1, len(array)):
            totalOfTwo = array[i] + array[x]
            if (totalOfTwo == target):
               rsts.append((array[i], array[x])) # add answer to rsts
    return -rsts
result = twoSum (array, target)

如果我们没有正确的答案,结果是一个空列表([]),那么

if result != []: # changed -1 with []
    print ("the intergers numbers meet target", result)
else:
    print ("result is not in range")

实现这一点最具python风格的方式是作为列表理解,它只是一行

>>> [(a[i],a[j]) for i in range(0, len(a)-1) for j in range(i+1, len(a)-1) if a[i]+a[j]==target]

[(4, 5), (10, -1)]

(请注意,这有助于提前终止,但您可以只分割第[0]个元素。)

关于您的代码/函数方法,我将其改写为(下次请要求对CodeReview.SE上的工作代码进行审查,而不是在这里进行审查):

def two_sum (a, target):
    for i in range(0, len(a)):
        for j in range( i + 1, len(a)):
            if a[i] + a[j] == target:
                return (a[i], a[j])
    return None

result = two_sum (a, target)

if result:
    print ("the numbers sum to target", result)
else:
    print ("result is not in range")

评论:

  • 由于您调用第一个索引i,调用第二个索引j,而不是x,这就不清楚x是值还是索引
  • 返回None比返回-1这样的哨兵值更像python。然后调用方只需使用if result:测试返回值,您不需要任何笨重的if result != -1if result is None
  • twoSum重命名为two_sum以遵循函数名和变量的Python命名约定(PEP-8):小写字母带下划线
  • 注意,此实现不会提前终止。但是,如果您只是将return (a[i], a[j])更改为yield (a[i], a[j]),这将使它成为一个按顺序返回所有(/any)匹配元组的生成器。(您需要将return None替换为yield StopIteration
  • 类似地totalOfTwo将被称为total_of_twopair_sum
  • 但是不需要为totalOfTwopairsList声明临时变量,直接使用表达式即可
  • 如果您来自Java,建议您学习正确的Python术语pairsList不是列表,而是元组array不是数组,而是列表。但我只能称之为a
  • 注意,我们只需要运行左索引i到len(a)-1而不是len(a),因为我们知道需要j来索引其右侧的元素

相关问题 更多 >