Python 3.x 在将元组附加到列表时,我可以附加(a+;b+;c)或(a,b+;c)或(a+;b,c),但附加(a,b,c)会导致程序拒绝运行
这是密码Python 3.x 在将元组附加到列表时,我可以附加(a+;b+;c)或(a,b+;c)或(a+;b,c),但附加(a,b,c)会导致程序拒绝运行,python-3.x,list,tuples,append,Python 3.x,List,Tuples,Append,这是密码 def check_right_angle(a, b, c): if a**2 + b**2 == c**2: return True return False def mn_to_abc(m, n): return m**2 - n**2, 2 * m * n, m**2 + n**2 list_solutions = [] for i in range(1001): #Getting all primitive triples usin
def check_right_angle(a, b, c):
if a**2 + b**2 == c**2:
return True
return False
def mn_to_abc(m, n):
return m**2 - n**2, 2 * m * n, m**2 + n**2
list_solutions = []
for i in range(1001): #Getting all primitive triples using Euclid's formula <= 1000
list_solutions.append([])
if i == 0:
continue
for m in range(1, int(i/2) - 1):
n = int(i / (2 * m) - m)
if m > n and n > 0:
a, b, c = mn_to_abc(m, n)
if check_right_angle(a, b, c) and a + b + c == i:
list_solutions[i].append((a, b, c))
for item in list_solutions: #Getting the remaining triples by using the primitive triples
for abc in item:
for i in range(1, 85): # 85 since 3x + 4x + 5x = 1000 => x = 83.3333 = 84
try:
new_a = abc[0] * i
new_b = abc[1] * i
new_c = abc[2] * i
if new_a + new_b + new_c <= 1000:
list_solutions[new_a + new_b + new_c].append((new_a, new_b, new_c))
else:
break
except:
continue
print(len(list_solutions[120]))
print(list_solutions[120])
我甚至尝试将其附加为列表而不是元组,但没有效果。遇到这样一件奇怪的事情。没关系,伙计们,刚刚有了顿悟。事实证明,添加到正在迭代的列表中是一个非常糟糕的想法。在第30行之前,我添加了以下代码:
if not (new_a, new_b, new_c) in list_solutions[new_a + new_b + new_c]:
您可能已经注意到,我仍在添加到我正在迭代的同一个列表中,但出于某种原因,只要该列表中的项目不重复,一切都很好。
我现在想结束这个问题,但它告诉我,我只能在2天内接受我自己的答案。请向Stack提供错误,我没有收到错误。代码一直在无限期地运行。即使按ctrl+c组合键也不能阻止它,我必须关闭vscode。我刚刚弄明白它为什么不起作用了,谢谢你
if not (new_a, new_b, new_c) in list_solutions[new_a + new_b + new_c]: