从列表中找到两个数字,这些数字加起来等于一个特定的数字 [英] Find two numbers from a list that add up to a specific number
问题描述
这是非常糟糕且混乱的地方,我是新来的,请帮助我.
This is super bad and messy, I am new to this, please help me.
基本上,我试图从列表中找到两个数字,这些数字加起来等于目标数字.
Basically, I was trying to find two numbers from a list that add up to a target number.
我用lst = [2, 4, 6, 10]
和目标值target = 8
设置了一个示例.在此示例中的答案将是(2, 6)
和(6, 2)
.
I have set up an example with lst = [2, 4, 6, 10]
and a target value of target = 8
. The answer in this example would be (2, 6)
and (6, 2)
.
下面是我的代码,但是它又长又丑,我敢肯定有更好的方法.您能否从下面的代码中看到我该如何改进?
Below is my code but it is long and ugly and I am sure there is a better way of doing it. Can you please see how I can improve from my code below?
from itertools import product, permutations
numbers = [2, 4, 6, 10]
target_number = 8
two_nums = (list(permutations(numbers, 2)))
print(two_nums)
result1 = (two_nums[0][0] + two_nums[0][1])
result2 = (two_nums[1][0] + two_nums[1][1])
result3 = (two_nums[2][0] + two_nums[2][1])
result4 = (two_nums[3][0] + two_nums[3][1])
result5 = (two_nums[4][0] + two_nums[4][1])
result6 = (two_nums[5][0] + two_nums[5][1])
result7 = (two_nums[6][0] + two_nums[6][1])
result8 = (two_nums[7][0] + two_nums[7][1])
result9 = (two_nums[8][0] + two_nums[8][1])
result10 = (two_nums[9][0] + two_nums[9][1])
my_list = (result1, result2, result3, result4, result5, result6, result7, result8, result9, result10)
print (my_list)
for i in my_list:
if i == 8:
print ("Here it is:" + str(i))
推荐答案
对于列表中的每个数字,您都可以查找他的补码(该数字与前一个数字相加即可得出所需的target
和).如果存在,请获取该对并退出,否则继续前进.
For every number on the list, you can look for his complementary (number that when added to the previous one would give the required target
sum). If it exists, get the pair and exit, otherwise move on.
如下所示:
numbers = [2, 4, 6, 10]
target_number = 8
for i, number in enumerate(numbers[:-1]): # note 1
complementary = target_number - number
if complementary in numbers[i+1:]: # note 2
print("Solution Found: {} and {}".format(number, complementary))
break
else: # note 3
print("No solutions exist")
产生:
Solution Found: 2 and 6
注意:
Notes:
- 您不必检查最后一个号码;如果有一对,那么到那时您已经找到了.
- 请注意,由于优化了成员资格检查(在列表中这非常昂贵),因为它仅考虑切片
numbers[i+1:]
.先前的号码已经过检查.切片的积极作用是,列表中存在一个4
,而对于8
的目标值并没有给出一对. - 这是一个很好的设置,可以解释在
for
循环中对else
的理解和误解.else
仅在循环不是由break
突然结束的情况下触发.
- You do not have to check the last number; if there was a pair you would have already found it by then.
- Notice that the membership check (which is quite costly in lists) is optimized since it considers the slice
numbers[i+1:]
only. The previous numbers have been checked already. A positive side-effect of the slicing is that the existence of one4
in the list, does not give a pair for a target value of8
. - This is an excellent setup to explain the miss-understood and often confusing use of
else
infor
-loops. Theelse
triggers only if the loop was not abruptly ended by abreak
.
如果即使列表中有单个 4
,即使您接受4
-4
解决方案,也可以进行以下修改:
If the 4
- 4
solution is acceptable to you even when having a single 4
in the list you can modify as follows:
numbers = [2, 4, 6, 10]
target_number = 8
for i, number in enumerate(numbers):
complementary = target_number - number
if complementary in numbers[i:]:
print("Solution Found: {} and {}".format(number, complementary))
break
else:
print("No solutions exist")
这篇关于从列表中找到两个数字,这些数字加起来等于一个特定的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!