贴问题

nums = range(5)#range is a built-in function that creates a list of integers
print(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 slice
print(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,解决方法的更多相关文章

  1. TypeError: 'range' object does not support item assignment处理方法

    vectorsum.py#!/usr/bin/env/pythonimport sysfrom datetime import datetimeimport numpy as np # def num ...

  2. TypeError: 'range' object does not support item assignment

    TypeError: 'range' object does not support item assignment I was looking at some python 2.x code and ...

  3. Python的字符串修改报错:TypeError: 'str' object does not support item assignment

    Python中想修改字符串的最后一个字符,使用name[-1] = 'e'来实现,运行后报错. 报错内容是:TypeError: 'str' object does not support item ...

  4. python3中报错:TypeError: 'range' object doesn't support item deletion

    1.源代码 以下代码执行时会报  range' object does not support item assignment 的错误,问题出现在第17行的runge(10): import unit ...

  5. TypeError: 'range' object doesn't support item deletion

    python 是个逐步迭代开发的过程,他不是向下兼容的,更不是向上兼容,版本不一致,好端端的程序就是不能运行了. 下面是在python 2中能运行,在Python 3中不能运行的代码.其实也很简单.但 ...

  6. Python:TypeError: 'range' object doesn't support item deletion

    报错代码: dataIndex = range(m) del (dataIndex[randIndex]) 报错信息: 错误原因: python3 range返回的是range对象,不是数组对象 解决 ...

  7. Python报错UnicodeDecodeError: ascii codec can t decode byte 0xe0 ...解决方法

    用命令(python setup.py install)安装webpy时候总是报错 在网上搜索到的解决方法如下: 1. 这是Python 2 mimetypes的bug 2. 需要将Python2.7 ...

  8. TypeError: 'str' object does not support item assignment Python常见错误

    1.string是一种不可变的数据类型 2.尝试使用 range()创建整数列 有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式. 需要记住 range() 返回的 ...

  9. Python报错:SyntaxError: Non-ASCII character '\xe5' in file的解决方法

    SyntaxError: Non-ASCII character '\xe5' in file 原因:Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他的 ...

随机推荐

  1. DDD领域驱动实践记录

    虽然很早之前就已经了解过DDD相关的内容了,但一方面网上理论知识太过碎片化导致难以理解,另一方面实践内容太少导致想动手的时候无从下手.于是就渐渐淡忘了这方面实践的念头. 最近重新了解了DDD相关的知识 ...

  2. .Net基础篇_学习笔记_第七天_计算质数(找出0-100以内说有质数)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Java中关于泛型集合类存储的总结

    集合类存储在任何编程语言中都是很重要的内容,只因有这样的存储数据结构才让我们可以在内存中轻易的操作数据,那么在Java中这些存储类集合结构都有哪些?内部实现是怎么样?有什么用途呢?下面分享一些我的总结 ...

  4. Aspose.Cell导出带chart图表

    最终实现的效果就是这样,代码比较多,我放在了CSDN上了,是无需模板的,别听网上瞎吹,说什么要模板,地址是:https://download.csdn.net/download/chanelwtt/1 ...

  5. 使用uEdit时,在线管理图片功能不可用

    把所有的配置文件都配置好了,uedit的在线管理功能图片还是不可用,看了一下源码: 是的,它在img的src属性后边的图片上加上了参数,那肯定不能用啊,所以修改源文件image.js.直接搜索noCa ...

  6. 读《深入理解Elasticsearch》点滴-改正用户拼写错误

    1.使用“建议”的方法:在query body的json结构体中,增加suggest节点:或者使用特殊的REST端点 2.es自带有多个不同的suggest实现,用来纠正用户的拼写错误及创建自动补全等 ...

  7. c# 9.0 特性提案 简化空参数验证代码

    简而言之就是将已存在的特性null参数验证,使用一个简单的语义标注进一步简化. 对于如下这段代码 // Before void Insert(string s) { if (s is null) { ...

  8. 一个低级错误引发Netty编码解码中文异常

    前言 最近在调研Netty的使用,在编写编码解码模块的时候遇到了一个中文字符串编码和解码异常的情况,后来发现是笔者犯了个低级错误.这里做一个小小的回顾. 错误重现 在设计Netty的自定义协议的时候, ...

  9. cvc-complex-type.2.3: Element 'dependency' cannot have character [children], because the type's cont

    直接复制网上的pom引入,报错 解决:自己手动输入一遍,不用直接复制,因为复制的时候,项目中编码跟网页上编码不一致,很容易导致出问题.

  10. springboot 2.1.3.RELEASE版本解析.properties文件配置

    1.有时为了管理一些特定的配置文件,会考虑单独放在一个配置文件中,如redis.properties: #Matser的ip地址 redis.host=192.168.5.234 #端口号 redis ...