我正在研究一个复制Eratosthenes Sieve的类,并且已获取给出的错误消息标题。以下是我的代码,更多问题随之而来。Python 3.x - 无法修复错误:“切片索引必须是整数或无或有__index__方法”+更多
class Sieve:
def __init__(self, max):
if max < 0:
raise RuntimeError
else:
self.numbers = [([False] * 2) + ([True] * (max - 1))]
def findPrimes(self):
for i in self.numbers:
if self.numbers:
for j in self.numbers[i:]:
if j % i == 0:
self.numbers[j] = False
else:
None
else:
None
def howMany(self): ##Must use reduce
reduce((lambda i: True if numbers else False), self.numbers, self.numbers[2:])
def toList(self):
T = [L[i] for i in self.numbers]
return print(self.numbers)
所以在功能findPrimes(个体),特别是在线路14,I得到标题所述的错误消息。问题的根源究竟是什么?
此外我想问几个关于我的方法的问题。在findPrimes中,我试图访问列表中的每个元素。如果确实如此,我想采用索引号(i)并访问所有其他指数(i,i + i,i + i + i等)的倍数。我还没有完全测试过,但我觉得我的语法错了。任何帮助表示赞赏。
最后,我需要在方法howMany中使用reduce函数来确定列表中有多少元素为真,并返回该数字。再次,任何有关该主题的知识非常感谢。
谢谢大家:)
运行脚本时,我得到'IndentationError:unexpected indent'。 –
检查'self.numbers'。这不是你的想法。 – user2357112
@ user2357112恐怕我不明白你在想什么。 –