小编Ser*_*nko的帖子

Python。使用切片替换元素

我需要实现 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)

也许你知道这个任务的一个很好的解决方案。

python replace list slice

2
推荐指数
1
解决办法
2880
查看次数

标签 统计

list ×1

python ×1

replace ×1

slice ×1