小弟新学Python~
先前在循环内使用list.append在list中append list
output出来的值 不知为何会一直重复
并没有依序加在list的最尾端
请教各位大大~小弟是哪个地方没有注意到
Output:
Loop 1
Need to add [-1, 0, 1]
Result [[-1, 0, 1]]
Loop 2
Need to add [-1, 0, 1]
Result [[-1, 1, 0], [-1, 1, 0]]
Loop 3
Need to add [-1, -1, 2]
Result [[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]]
.
.
.
.
////////////////////////////////////////////////////////////////////
Code:
nums = [-1, 0, 1, 2, -1, -4]
if len(nums) < 3:
final_answer_list = []
else:
answer_list = [0,0,0]
final_answer_list = []
print(final_answer_list)
input_length = len(nums)
z = 0
for i in range(0,input_length):
for j in range(0,input_length):
if j!=i:
for k in range(0,input_length):
if k!=i and k!=j:
answer_list[0] = nums[i]
answer_list[1] = nums[j]
answer_list[2] = nums[k]
cum_val = 0
for l in range(0,3):
cum_val += answer_list[l]
if cum_val == 0:
z += 1
print('Loop',z)
answer_list2 = sorted(answer_list)
print('Need to add ',answer_list2)
final_answer_list.append(answer_list)
print('Result ',final_answer_list)