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

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

# def numpysum(n):#     a = np.arange(n) ** 2#     b = np.arange(n) ** 3#     c = a + b#     return c

def pythonsum(n):    a = range(n)    b = range(n)    c = []    for i in range(len(a)):        a[i] = i ** 2        b[i] = i ** 3        c.append(a[i] + b[i])    return c

size = int(sys.argv[1])print (size)

start = datetime.now()c = pythonsum(size)delta = datetime.now() - startprint ("The last 2 elements of the sum", c[-2:])print ("PythonSum elapsed time in microseconds", delta.microseconds)# start = datetime.now()# c = numpysum(size)# delta = datetime.now() - start# print ("The last 2 elements of the sum", c[-2:])# print ("NumPySum elapsed time in microseconds", delta.microseconds)----------------------------------------------------------------------------运行结果:

D:\>python vectorsum.py 1000
1000
Traceback (most recent call last):
File "vectorsum.py", line 26, in <module>
c = pythonsum(size)
File "vectorsum.py", line 17, in pythonsum
a[i] = i ** 2
TypeError: ‘range‘ object does not support item assignment

----------------------------------------------------------------------------------------

pythonsum函数修改如下
def pythonsum(n):    a = list(range(n))    b = list(range(n))    c = []    for i in range(len(a)):        a[i] = i ** 2        b[i] = i ** 3        c.append(a[i] + b[i])    return c

运行正常,通过-----------------------------------------------------------
 
 

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

原文地址:https://www.cnblogs.com/master-road/p/10646590.html

时间: 2024-10-18 02:20:00

TypeError: 'range' object does not support item assignment处理方法的相关文章

python 报错TypeError: &#39;range&#39; object does not support item assignment,解决方法

贴问题 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

TypeError: &#39;range&#39; object doesn&#39;t support item deletion

python 是个逐步迭代开发的过程,他不是向下兼容的,更不是向上兼容,版本不一致,好端端的程序就是不能运行了. 下面是在python 2中能运行,在Python 3中不能运行的代码.其实也很简单.但是这些边边角角的东西着实让人头疼. >>> a=range(10)>>> arange(0, 10)>>> del[a[1]]Traceback (most recent call last):  File "<pyshell#6>&

TypeError: &#39;list&#39; object cannot be interpreted as an integer

TypeError: 'list' object cannot be interpreted as an integer 类型错误,不能将list对象转换为一个整数. 错误代码,例如如下例子: args = [3,6] print(list(range(args))) range函数本应该需求,一个整数,或者一对范围,或者三个整数类型,才能构造一个iterable,这里直接将args这个列表传递给它是不行的,需要通过解压缩机制,更正后代码为: args = [3,6] print(list(ra

TypeError:&#39;dict&#39; object is not callable

TypeError:'dict' object is not callable 出现这种错误有两种可能: 1. 代码里重新定义了dict,比如 dict= {...},这时调用的是代码里定义的dict而不是python内置类型 2. 取字典内容时用了()而不是[].比如sdict("content_id"),应该是sdict["content_id"] TypeError:'dict' object is not callable

python框架Scrapy报错TypeError: &#39;float&#39; object is not iterable解决

原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: 1 pip3 install Twisted==16.6.0 2 3 注:Twisted16.6.0安装后,会自动卸载高版本的Twisted python框架Scrapy报错TypeError: 'float' object is not iterable解决

TypeError: &#39;NoneType&#39; object has no attribute &#39;__getitem__&#39;

Buildbot版本:(0.8.10) Bug:TypeError: 'NoneType' object has no attribute '__getitem__' 参考页面:http://trac.buildbot.net/ticket/3107?cversion=0&cnum_hist=3 修改文件slaves.py中出错部分的代码为 1 try: 2 max_builds = int(request.args.get('numbuilds', ['10'])[0]) 3 except (

django &#39;set&#39; object does not support indexing

在定义Model之后使用syncdb 同步的时候报出这个错误,检查之后发现是用错了.在model的Meta里面,排序这些用的应该是 ordering = ['last','first','middle'] unique_together = ['first','last','middle'] verbose_name_plural = "people" 如果是误写成 ordering = {'last','first','middle'}        unique_together

python TypeError: &#39;builtin_function_or_method&#39; object is not iterable keys

statinfo = os.stat( OneFilePath ) if AllFiles.has_key( statinfo.st_size ): OneKey = AllFiles[ statinfo.st_size ] OneKey.append( OneFilePath ) AllFiles[ statinfo.st_size ] = OneKey else: if statinfo.st_size > MinSize: # print statinfo.st_size AllFiles

TypeError: &#39;str&#39; object is not callable

Python报错TypeError: 'str' object is not callable TypeError: 'str' object is not callable