给定一个列表为nums = [2, 7, 11, 15],目标值target = 9,找出列表中任意2数之和等于9的元素以及所在位置
思路:双重遍历去一对一的比较判断
1 nums = [2, 7, 11, 15, 1, 8, 2] 2 target = 9 3 list_new = [] 4 def find_num_indx(): 5 6 for i in range(len(nums)): 7 for j in range(i,len(nums)): #从i开始 8 if nums[i]+nums[j] == target: 9 z = zip((nums[i],i),(nums[j],j)) #打包成一组tuple(元素)和(索引) 10 list_new.append(list(z)) # 强制转为一组list 11 return list_new 12 13 print(find_num_indx())
标签:11,target,nums,Python,列中,list,列表,目标值,new From: https://www.cnblogs.com/hnfangh/p/17520544.html