亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

給定一個整數數組,返回兩個數字的索引

給定一個整數數組,返回兩個數字的索引

拉莫斯之舞 2023-06-06 16:37:11
給定一個整數數組,返回兩個數字的索引,使它們加起來等于一個特定的目標。您可能會假設每個輸入只有一個解決方案,并且您可能不會兩次使用相同的元素。例子:給定 nums = [2, 7, 11, 15], target = 9,因為 nums[0] + nums[1] = 2 + 7 = 9,返回 [0, 1]。
查看完整描述

3 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

from itertools import combinations

import numpy as np


N=9 

# your set of numbers

nums = np.array([2,7,11,15])


# prepare list of all possible combinations of two numbers using built-in generator

combs = [i for i in combinations(nums,2)]


# sum up these two numbers

sums = np.sum(combs, axis=1)


# find index of wanted summed of the two numbers in sums

good_comb = np.where(sums==N)[0][0]


# search the indices in your original list, knowing index in combs

indices_sum_to_N = [np.where(nums==i)[0][0] for i in combs[good_comb]]

打?。╥ndices_sum_to_N)


查看完整回答
反對 回復 2023-06-06
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

您可以使用 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)


查看完整回答
反對 回復 2023-06-06
?
MM們

TA貢獻1886條經驗 獲得超2個贊

像這種將值和索引作為字典返回的蠻力方法怎么樣?


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]}}

或者沒有 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)


查看完整回答
反對 回復 2023-06-06
  • 3 回答
  • 0 關注
  • 202 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號