我需要实现 Eratosthenes 算法的筛分。我有列表:
bar = [2, 3, 4, 5, 6, 7, 8, 9, 10]
我需要用“0”替换每个奇数元素。现在我有代码:
while viewIndex < maxNumber:
bar[viewIndex] = 0
viewIndex += 2
Run Code Online (Sandbox Code Playgroud)
但我记得切片。对我来说,写这样的东西会很好:
bar[currentIndex::2] = 0
Run Code Online (Sandbox Code Playgroud)
但我有错误:
TypeError: must assign iterable to extended slice
Run Code Online (Sandbox Code Playgroud)
也许你知道这个任务的一个很好的解决方案。