给定一个整数数组,返回两个数字的索引

给定一个整数数组,返回两个数字的索引,使它们加起来等于一个特定的目标。

您可能会假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素。

例子:

给定 nums = [2, 7, 11, 15], target = 9,

因为 nums[0] + nums[1] = 2 + 7 = 9,返回 [0, 1]。


拉莫斯之舞
浏览 179回答 3
3回答

德玛西亚99

from itertools import combinationsimport numpy as npN=9 # your set of numbersnums = np.array([2,7,11,15])# prepare list of all possible combinations of two numbers using built-in generatorcombs = [i for i in combinations(nums,2)]# sum up these two numberssums = np.sum(combs, axis=1)# find index of wanted summed of the two numbers in sumsgood_comb = np.where(sums==N)[0][0]# search the indices in your original list, knowing index in combsindices_sum_to_N = [np.where(nums==i)[0][0] for i in combs[good_comb]]打印(indices_sum_to_N)

万千封印

您可以使用 enumerate 来获取符合您需要的条件的索引。下面的答案只是使用枚举的蛮力解决方案。它将返回列表列表。请注意,索引比较只是遍历对角线上的项目并且没有重复(例如,如果 [1,2] 在结果 [2,1] 中则不会)。result = []for index1, value1 in enumerate(items):    for index2, value2 in enumerate(items):          if index1>index2 and (value1 + value2)==target:              result.append([index1, index2])print(result)

MM们

像这种将值和索引作为字典返回的蛮力方法怎么样?import itertoolsdef get_indexs_of_sum(search_list, search_val):&nbsp;&nbsp;&nbsp; &nbsp; #remove any values higher than the search val&nbsp; &nbsp; search_list = [val for val in search_list if val <= search_val]&nbsp; &nbsp; #get all combinations with 2 elements&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; combinations = list(itertools.combinations(search_list, 2))&nbsp; &nbsp; #check which lines sum the search val&nbsp; &nbsp; totals = [comb for comb in combinations if sum(comb) == search_val]&nbsp; &nbsp; #get indexes of answers&nbsp; &nbsp; answerDict = {}&nbsp; &nbsp; #loop answers get indexes and store in a dictionary&nbsp; &nbsp; for i,answer in enumerate(totals):&nbsp; &nbsp; &nbsp; &nbsp; indexes = []&nbsp; &nbsp; &nbsp; &nbsp; for list_item in answer:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; indexes.append(search_list.index(list_item))&nbsp; &nbsp; &nbsp; &nbsp; answerDict[i] = {"Values":answer,"Index":indexes}&nbsp; &nbsp; return answerDict#GET THE LIST AND THE SEARCH VALUEsearch_list = [19,15,1,13,1,3,16,7,11,10,16, 12, 18, 3, 15, 18, 7, 17, 6, 1, 2, 17, 1, 4, 12, 2, 14, 8, 10, 9, 19, 1, 18, 2, 3, 18, 3, 6, 6, 9]&nbsp; &nbsp;&nbsp;search_val = 21&nbsp; &nbsp;&nbsp;#call the functionanswers = get_indexs_of_sum(search_list, search_val)和输出{0: {'Values': (19, 2), 'Index': [0, 20]},&nbsp;1: {'Values': (19, 2), 'Index': [0, 20]},&nbsp;2: {'Values': (19, 2), 'Index': [0, 20]},&nbsp;3: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;4: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;5: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;6: {'Values': (13, 8), 'Index': [3, 27]},&nbsp;7: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;8: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;9: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;10: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;11: {'Values': (7, 14), 'Index': [7, 26]},&nbsp;12: {'Values': (11, 10), 'Index': [8, 9]},&nbsp;13: {'Values': (11, 10), 'Index': [8, 9]},&nbsp;14: {'Values': (12, 9), 'Index': [11, 29]},&nbsp;15: {'Values': (12, 9), 'Index': [11, 29]},&nbsp;16: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;17: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;18: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;19: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;20: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;21: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;22: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;23: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;24: {'Values': (15, 6), 'Index': [1, 18]},&nbsp;25: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;26: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;27: {'Values': (7, 14), 'Index': [7, 26]},&nbsp;28: {'Values': (17, 4), 'Index': [17, 23]},&nbsp;29: {'Values': (2, 19), 'Index': [20, 0]},&nbsp;30: {'Values': (17, 4), 'Index': [17, 23]},&nbsp;31: {'Values': (12, 9), 'Index': [11, 29]},&nbsp;32: {'Values': (12, 9), 'Index': [11, 29]},&nbsp;33: {'Values': (2, 19), 'Index': [20, 0]},&nbsp;34: {'Values': (19, 2), 'Index': [0, 20]},&nbsp;35: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;36: {'Values': (18, 3), 'Index': [12, 5]},&nbsp;37: {'Values': (3, 18), 'Index': [5, 12]},&nbsp;38: {'Values': (18, 3), 'Index': [12, 5]}}或者没有 itertools:def get_indexs_of_sum_no_itertools(search_list, search_val):&nbsp;&nbsp;&nbsp; &nbsp; answers = []&nbsp; &nbsp; #loop list once&nbsp; &nbsp; for i,x in enumerate(search_list):&nbsp; &nbsp; &nbsp; &nbsp; #loop list twice&nbsp; &nbsp; &nbsp; &nbsp; for ii,y in enumerate(search_list):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #if indexes aren't dupes and value matches:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if x != y and x + y == search_val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answers.append({"indexes":(i,ii),"values":(x,y)})&nbsp; &nbsp; return answerssearch_list = [19,15,1,13,1,3,16,7,11,10,16, 12, 18, 3, 15, 18, 7, 17, 6, 1, 2, 17, 1, 4, 12, 2, 14, 8, 10, 9, 19, 1, 18, 2, 3, 18, 3, 6, 6, 9]search_val = 14ans = get_indexs_of_sum_no_itertools(search_list,search_val)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python