python 报错TypeError: 'range' object does not support item assignment,解决方法,,贴问题nums =
python 报错TypeError: 'range' object does not support item assignment,解决方法,,贴问题nums =
贴问题
nums = range(5)#range is a built-in function that creates a list of integersprint(nums)#prints "[0,1,2,3,4]"print(nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints ‘[2,3]"print(nums[2:])#Get a slice from index 2 to the end; prints "[2,3,4]"print(nums[:2])#Get a slice from the start to index 2 (exclusive); prints "[0,1]"print(nums[:])#Get a slice of the whole list ; prints "[0,1,2,3,4]"print(nums[:-1])#Slice indices can be negative; prints "[0,1,2,3]"nums[2:4] = [8,9] # Assign a new sublist to a sliceprint(nums)#prints "[0,1,8,9,4]"
2.报错的原因:
尝试使用range()
创建整数列表(导致“TypeError: ‘range’ object does not support item assignment”)有时你想要得到一个有序的整数列表,所以range() 看上去是生成此列表的不错方式。然而,你需要记住range() 返回的是“range object”,而不是实际的list 值。
3.解决方法:
将上面例子的代码: nums = range(5)改为nums = list(range(5))
python 报错TypeError: 'range' object does not support item assignment,解决方法
相关内容
- python第三方库PrettyTable使用实例,,一、简介Pretty
- Python---老王开枪,,class Pers
- python实现去重排序,,功能要求: 明
- 如何用python 在视频上添加自己的logo,,头条号:https:
- Python3安装geohash,,Geohash是一个
- [python]命令窗口显示不是内部或外部命令的原因和解决
- 【Python web 开发】微博授权登录,获取access_token,,1、
- 图像标注工具labelImg安装方法(win7+Python3.5+Qt5),labe
- Python之如何删除pandas DataFrame的某一/几列,,删除pandas
- 关于python使用requests依赖包时出现版本不匹配的警告问
评论关闭