python 报错TypeError: 'range' object does not support item assignment,解决方法
2016-03-07 15:23
871 查看
1. 例子如下:
from math import sqrt if __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)) 就好啦!相关文章推荐
- Objective-C适用C数学函数 <math.h>
- Objective-C中的meta-class
- Objective-C中@property的所有属性详解
- NSObject的load和initialize方法
- Jsionarray和jsionobject-----java
- Objective-C关于非ARC模式下的对象引用计数
- NSObject的load和initialize方法
- Objective-C Runtime 1小时入门教程
- Objective-C中通过下标的方式访问自定义数据模型中属性
- Objective-C学习记录
- Objective-c 内存管理
- Objective-c (多输入参数的方法)
- Object-c基础(2)
- Func<T, bool> Func<T, object>小例子
- Objective-C语法之代码块(block)的使用
- enumerateObjectsUsingBlock 、for 、for(... in ...) 的差别 & 性能測试
- kobject && kset
- iOS学习笔记(13)—— Objective-C类扩展的方法之一——分类(category)
- Object类的重要方法
- Keras笔记 -- objective