我对 Python 还是很陌生,很难在我的列表中计算我的相等数量。我创建了一个数字列表(list oneven),根据哥德巴赫的说法,每个数字都等于三个素数。我现在有一个所有素数组合的列表,现在我想计算列表中每个数字的组合数量,并将其打印出来。我尝试使用“导入集合”,但由于我的代码不可散列而无法使用。然后我厌倦了将一个数字添加到一个空列表中,该数字上升到相等的总和,但我收到错误消息:
IndexError:列表分配索引超出范围
这是我正在苦苦挣扎的代码:
lijst2 = []
lijst = []
for i in oneven:
for a in priemgetallen:
for b in priemgetallen:
if a >= b:
c = i - a - b
if c in priemgetallen and b >= c:
lijst.append([c,b,a])
for item in lijst:
if sum(item) in lijst2:
lijst2[sum(item)] = lijst2.get(sum(item))+1
else:
lijst2[sum(item)] = 1
for k,v in lijst2.items():
print(str(k)+':'+str(v))
lijst2 = set(lijst)
print(lijst2)
如果您对我正在尝试做的事情感兴趣,我正在尝试为哥德巴赫理论编写一个计数器,所以这是我的整个代码:
oneven = []
for i in range(7,102,2):
oneven.append(i)
priemgetallen = [2]
counter = 3
while priemgetallen[-1] < oneven[-1]:
priemgetallendelers = []
for i in range (1,counter+1):
if counter % i == 0:
priemgetallendelers.append(i)
if len(priemgetallendelers) == 2:
priemgetallen.append(counter)
counter += 1
else:
counter +=1
lijst2 = []
lijst = []
for i in oneven:
for a in priemgetallen:
for b in priemgetallen:
if a >= b:
c = i - a - b
if c in priemgetallen and b >= c:
lijst.append([c,b,a])
for item in lijst:
if sum(item) in lijst2:
lijst2[sum(item)] = lijst2.get(sum(item))+1
else:
lijst2[sum(item)] = 1
for k,v in lijst2.items():
print(str(k)+':'+str(v))
lijst2 = set(lijst)
print(lijst2)
最后它应该看起来像这样:
7 = 2 + 2 + 3
9 = 2 + 2 + 5
= 3 + 3 + 3
11 = 2 + 2 + 7
= 3 + 3 + 5
13 = 3 + 3 + 7
= 3 + 5 + 5
Options to write: 7, 9, 11, ...:
1, 2, 2, 2, 3, 4, 3, 5, 5, 5, 7, 7, 6, 9, 8,