代码如下:
def total(nums, target):
num_copy = tuple(nums)
print(num_copy)
index = []
for i in nums:
another_num = target-i
nums.remove(i)
if another_num in nums:
ele = (i, another_num)
for j in ele:
index.append(num_copy.index(j))
return index
print(total([2, 7, 11, 6, 3, 15, 16, 17], 9))
问题是:循环这个列表:[2, 7, 11, 6, 3, 15, 16, 17]的时候,为什么没有所有元素都循环到? debug 模式显示只循环了[2, 11, 3, 16]这几个元素。而[7,6,15,17]这几个元素并没有循环。这是为什么?求知道的大神告知,感谢感谢!
def total(nums, target):
num_copy = tuple(nums)
print(num_copy)
index = []
for i in nums:
another_num = target-i
nums.remove(i)
if another_num in nums:
ele = (i, another_num)
for j in ele:
index.append(num_copy.index(j))
return index
print(total([2, 7, 11, 6, 3, 15, 16, 17], 9))
问题是:循环这个列表:[2, 7, 11, 6, 3, 15, 16, 17]的时候,为什么没有所有元素都循环到? debug 模式显示只循环了[2, 11, 3, 16]这几个元素。而[7,6,15,17]这几个元素并没有循环。这是为什么?求知道的大神告知,感谢感谢!