python 报错TypeError: 'range' object does not support item assignment,解决方法
来源:互联网 发布:windows无法启动怎么办 编辑:程序博客网 时间:2024/04/30 08:50
1. 例子如下:
from math import sqrtif __name__ == '__main__': N = 100 a = range(0,N) for i in range(2,int(sqrt(N))): for j in range(i + 1,N): if (a[i] != 0) and (a[j] != 0): if a[j] % a[i] == 0: a[j]= 0 for i in range(2,N): if a[i] != 0: print ("%5d" % a[i]) if (i - 2) % 10 == 0: print
执行后报错:TypeError: ‘range’ object does not support item assignment
2 .报错原因如下:
尝试使用range()
创建整数列表(导致“TypeError: ‘range’ object does not support item assignment”)有时你想要得到一个有序的整数列表,所以range() 看上去是生成此列表的不错方式。然而,你需要记住range() 返回的是“range object”,而不是实际的list 值。
3. 解决方法:
将上面例子的代码: a = range(0,N)改为a = list(range(0,N)) 就好啦!
1 0
- python 报错TypeError: 'range' object does not support item assignment,解决方法
- TypeError: 'str' object does not support item assignment 的解决方法
- python object does not support item assignment
- TypeError: 'ItemMeta' object does not support item assignment
- theano 中出现 TypeError: 'TensorVariable' object does not support item assignment
- python提示错误TypeError: 'dict_keys' object does not support indexing
- TypeError: 'dict_keys' object does not support indexing
- TypeError: 'dict_keys' object does not support indexing
- TypeError: 'dict_keys' object does not support indexing
- TypeError: 'dict_keys' object does not support indexing
- TypeError: 'dict_keys' object does not support indexing
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- python 3.6.2 TypeError: 'range' object doesn't support item deletion
- python3中报错:TypeError: 'range' object doesn't support item deletion
- python3中报错:TypeError: 'range' object doesn't support item deletion
- python3.6 TypeError: 'dict_keys' object does not support indexing
- Python报错TypeError: 'str' object is not callable
- Python报错TypeError: 'str' object is not callable
- 【nodejs】npm常用命令
- LeetCode 278. First Bad Version
- static的作用
- git使用一览
- Android开发之开机启动没有界面的应用程序
- python 报错TypeError: 'range' object does not support item assignment,解决方法
- 源码分析shiro认证授权流程
- SAS学习(1)
- 【Linux进程间通信】 - 匿名管道
- Xcode export/upload error: Your session has expired. Please log in
- 并发
- uboot 移植
- iOS 视图,动画渲染机制探究
- 下载SVN上的源代码?