简体   繁体   English

如何对单个列表中的两个元素求和并与 integer 参数进行比较

[英]How to sum two elements from a single list and compare to an integer parameter

I'm writing a function that checks to see if the sum of any two elements within the same list equals an integer argument.我正在编写一个 function 来检查同一列表中任何两个元素的总和是否等于 integer 参数。

For example: Given 17 and [10, 15, 3, 7], it should return True as 17 is 10 + 7.例如:给定 17 和 [10, 15, 3, 7],它应该返回 True,因为 17 是 10 + 7。

Given 17 and [10, 15, 4, 8], it should return False, as there is no pair in the list that sums to 17.给定 17 和 [10, 15, 4, 8],它应该返回 False,因为列表中没有总和为 17 的对。

I'm completely lost on how to approach this.我完全不知道如何处理这个问题。 Any help is appreciated!任何帮助表示赞赏!

for i in range(0, len(lst) - 1):
    return k == lst[i] + lst[i]

return lst

Given 17 and [10, 15, 3, 7], it should return True as 17 is 10 + 7.给定 17 和 [10, 15, 3, 7],它应该返回 True,因为 17 是 10 + 7。

Given 17 and [10, 15, 4, 8], it should return False, as there is no pair in the list that sums to 17.给定 17 和 [10, 15, 4, 8],它应该返回 False,因为列表中没有总和为 17 的对。

def check(n, lst):
    lst_set = set(lst)
    for i in lst:
        if (n-i) in lst_set: 
            if (n-i) != i:   # check to see if it returned itself (not a pair)
                return True
    return False

Here you have n as the goal number and then lst as your input.在这里,您将n作为目标编号,然后将lst作为您的输入。

You convert the input to a set first to speed up lookups, then for each one you take your goal number, subtract your current number, and see if it's in the set.您首先将输入转换为一个集合以加快查找速度,然后对于每个目标数字,减去您当前的数字,然后查看它是否在集合中。 If it is, then you have an answer True and if not, move on.如果是,那么您的答案True ,如果不是,请继续。 If you reach the end without finding one then you don't and return False如果你到达终点却没有找到一个,那么你不会并返回False

The main idea behind this is that when you take your goal number and subtract the number you are on now, then you get the number you need to make a pair.这背后的主要思想是,当你用你的目标号码减去你现在的号码时,你就会得到你需要的号码来做一对。 So you check your set to see if that number exists in it.因此,您检查您的设备以查看其中是否存在该数字。 You don't need to go through each potential pair this way.您不需要以这种方式通过每个电位对 go 。


Edit: If you want to handle duplicate inputs then you can build the set as you go which will be slower than the above answer which cannot handle duplicate inputs but will still be extremely fast compared to non-hash table implementations.编辑:如果您想处理重复输入,那么您可以像 go 一样构建集合,这将比上述无法处理重复输入的答案慢,但与非哈希表实现相比仍然非常快。

def check_withdup(n, lst):
    lst_set = set()   # start with blank set
    for i in lst:
        if (n-i) in lst_set:   # if it is in there, it wasn't itself so it is a pair
            return True
        else:
            lst_set.add(i)   # if it isn't in there, add it in
    return False

This will work in one iteration but the time to set.add() is slower than the time to create a set from the same list which is why the above will be faster than this.这将在一次迭代中起作用,但是set.add()的时间比从同一列表创建集合的时间慢,这就是为什么上面会比这更快的原因。

def check(n, lst):

    for i in lst:
        temp = lst.copy()
        temp.remove(i)
        if n-i in temp:
            return True
    return False


l = [10,6,7,5,10]

print(check(20,l))

this returns这返回

True
def checkSum(num, lst):
    for i in lst:
       for j in lst:
         if num==i + j:
            return True

return False

This way is not as efficient as the previous answer, but it is more a Brute Force approach comparing sum of all the pairs and just checking if they satisfy the condition.这种方法不如前面的答案有效,但它更像是一种蛮力方法,比较所有对的总和并检查它们是否满足条件。

Try this bro试试这个兄弟

 def check(n,lst):
        for item in lst:
            temp = item
            for i in range(len(lst)):
                if lst.index(temp) != i: #This is to avoid comparison with the same index
                    if n == temp + lst[i]:
                        print("MATCH FOUND! ", temp, " and ", lst[i])
                        return True
                    else:
                        continue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM