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

Python 给定一个整数数组,返回两个数的索引,python,Python,给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标 您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素 例如: 给定nums=[2,7,11,15],target=9 因为nums[0]+nums[1]=2+7=9, 返回[0,1]。您可以使用enumerate获取与所需条件匹配的索引。下面的答案只是使用enumerate的暴力解决方案。它将返回一个列表列表。请注意,索引比较只是在对角线上方的项目上进行,没有重复项(例如,如果[1,2]为结果[2,1],则不会重复) p

给定一个整数数组,返回两个数字的索引,使它们相加到一个特定的目标

您可以假设每个输入都有一个解决方案,并且不能两次使用同一个元素

例如:

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

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

您可以使用enumerate获取与所需条件匹配的索引。下面的答案只是使用enumerate的暴力解决方案。它将返回一个列表列表。请注意,索引比较只是在对角线上方的项目上进行,没有重复项(例如,如果[1,2]为结果[2,1],则不会重复)


print(indexes\u sum\u to\N)

像这个蛮力方法这样的方法怎么样,它以字典的形式返回值和索引

import itertools

def get_indexs_of_sum(search_list, search_val):  

    #remove any values higher than the search val
    search_list = [val for val in search_list if val <= search_val]

    #get all combinations with 2 elements    
    combinations = list(itertools.combinations(search_list, 2))

    #check which lines sum the search val
    totals = [comb for comb in combinations if sum(comb) == search_val]

    #get indexes of answers
    answerDict = {}

    #loop answers get indexes and store in a dictionary
    for i,answer in enumerate(totals):
        indexes = []
        for list_item in answer:
            indexes.append(search_list.index(list_item))

        answerDict[i] = {"Values":answer,"Index":indexes}

    return answerDict



#GET THE LIST AND THE SEARCH VALUE

search_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 = 21
    
#call the function
answers = get_indexs_of_sum(search_list, search_val)
或不使用itertools:

def get_indexs_of_sum_no_itertools(search_list, search_val):  
    answers = []
    #loop list once
    for i,x in enumerate(search_list):
        #loop list twice
        for ii,y in enumerate(search_list):
            #if indexes aren't dupes and value matches:
            if x != y and x + y == search_val:
                answers.append({"indexes":(i,ii),"values":(x,y)})
    return answers

search_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 = 14

ans = get_indexs_of_sum_no_itertools(search_list,search_val)

欢迎来到堆栈溢出。请读,拿着这个,读,还有这个。最后,请阅读?
import itertools

def get_indexs_of_sum(search_list, search_val):  

    #remove any values higher than the search val
    search_list = [val for val in search_list if val <= search_val]

    #get all combinations with 2 elements    
    combinations = list(itertools.combinations(search_list, 2))

    #check which lines sum the search val
    totals = [comb for comb in combinations if sum(comb) == search_val]

    #get indexes of answers
    answerDict = {}

    #loop answers get indexes and store in a dictionary
    for i,answer in enumerate(totals):
        indexes = []
        for list_item in answer:
            indexes.append(search_list.index(list_item))

        answerDict[i] = {"Values":answer,"Index":indexes}

    return answerDict



#GET THE LIST AND THE SEARCH VALUE

search_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 = 21
    
#call the function
answers = get_indexs_of_sum(search_list, search_val)
{0: {'Values': (19, 2), 'Index': [0, 20]},
 1: {'Values': (19, 2), 'Index': [0, 20]},
 2: {'Values': (19, 2), 'Index': [0, 20]},
 3: {'Values': (15, 6), 'Index': [1, 18]},
 4: {'Values': (15, 6), 'Index': [1, 18]},
 5: {'Values': (15, 6), 'Index': [1, 18]},
 6: {'Values': (13, 8), 'Index': [3, 27]},
 7: {'Values': (3, 18), 'Index': [5, 12]},
 8: {'Values': (3, 18), 'Index': [5, 12]},
 9: {'Values': (3, 18), 'Index': [5, 12]},
 10: {'Values': (3, 18), 'Index': [5, 12]},
 11: {'Values': (7, 14), 'Index': [7, 26]},
 12: {'Values': (11, 10), 'Index': [8, 9]},
 13: {'Values': (11, 10), 'Index': [8, 9]},
 14: {'Values': (12, 9), 'Index': [11, 29]},
 15: {'Values': (12, 9), 'Index': [11, 29]},
 16: {'Values': (18, 3), 'Index': [12, 5]},
 17: {'Values': (18, 3), 'Index': [12, 5]},
 18: {'Values': (18, 3), 'Index': [12, 5]},
 19: {'Values': (3, 18), 'Index': [5, 12]},
 20: {'Values': (3, 18), 'Index': [5, 12]},
 21: {'Values': (3, 18), 'Index': [5, 12]},
 22: {'Values': (15, 6), 'Index': [1, 18]},
 23: {'Values': (15, 6), 'Index': [1, 18]},
 24: {'Values': (15, 6), 'Index': [1, 18]},
 25: {'Values': (18, 3), 'Index': [12, 5]},
 26: {'Values': (18, 3), 'Index': [12, 5]},
 27: {'Values': (7, 14), 'Index': [7, 26]},
 28: {'Values': (17, 4), 'Index': [17, 23]},
 29: {'Values': (2, 19), 'Index': [20, 0]},
 30: {'Values': (17, 4), 'Index': [17, 23]},
 31: {'Values': (12, 9), 'Index': [11, 29]},
 32: {'Values': (12, 9), 'Index': [11, 29]},
 33: {'Values': (2, 19), 'Index': [20, 0]},
 34: {'Values': (19, 2), 'Index': [0, 20]},
 35: {'Values': (18, 3), 'Index': [12, 5]},
 36: {'Values': (18, 3), 'Index': [12, 5]},
 37: {'Values': (3, 18), 'Index': [5, 12]},
 38: {'Values': (18, 3), 'Index': [12, 5]}}
def get_indexs_of_sum_no_itertools(search_list, search_val):  
    answers = []
    #loop list once
    for i,x in enumerate(search_list):
        #loop list twice
        for ii,y in enumerate(search_list):
            #if indexes aren't dupes and value matches:
            if x != y and x + y == search_val:
                answers.append({"indexes":(i,ii),"values":(x,y)})
    return answers

search_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 = 14

ans = get_indexs_of_sum_no_itertools(search_list,search_val)