TypeError: 'range' object does not support item assignment处理方法
vectorsum.py
#!/usr/bin/env/python
import sys
from datetime import datetime
import 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() - start
print ("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处理方法的更多相关文章
- 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 ...
- python 报错TypeError: 'range' object does not support item assignment,解决方法
贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints ...
- python3中报错:TypeError: 'range' object doesn't support item deletion
1.源代码 以下代码执行时会报 range' object does not support item assignment 的错误,问题出现在第17行的runge(10): import unit ...
- TypeError: 'range' object doesn't support item deletion
python 是个逐步迭代开发的过程,他不是向下兼容的,更不是向上兼容,版本不一致,好端端的程序就是不能运行了. 下面是在python 2中能运行,在Python 3中不能运行的代码.其实也很简单.但 ...
- Python的字符串修改报错:TypeError: 'str' object does not support item assignment
Python中想修改字符串的最后一个字符,使用name[-1] = 'e'来实现,运行后报错. 报错内容是:TypeError: 'str' object does not support item ...
- TypeError: 'str' object does not support item assignment Python常见错误
1.string是一种不可变的数据类型 2.尝试使用 range()创建整数列 有时你想要得到一个有序的整数列表,所以 range() 看上去是生成此列表的不错方式. 需要记住 range() 返回的 ...
- Python:TypeError: 'range' object doesn't support item deletion
报错代码: dataIndex = range(m) del (dataIndex[randIndex]) 报错信息: 错误原因: python3 range返回的是range对象,不是数组对象 解决 ...
- python TypeError: 'str' object does not support item assignment”
想替换string里的空格,遍历替换提示如题错误,查询得知string类型不可更改 import string s = "2013/2/12" b = s.replace('/', ...
- TypeError: '_io.TextIOWrapper' object does not support item assignment
纯小白 遇到的细节问题: 报错 一开始看到这个傻逼了 TypeError: '_io.TextIOWrapper' object does not support item assignment 其实 ...
随机推荐
- pycharm中运行成功的python代码在jenkin中运行问题总结
我们在用selenium+python完成了项目的UI自动化后,一般用jekins持续集成工具来定期运行,python程序在pycharm中编辑运行成功,但在jenkins中运行失败的两个问题,整理如 ...
- redis bind的坑
启动redis时,发现外网访问不了 检查以下方面 1. ping redis 的ip 2. 检查防火墙端口是否开放3. bind bind bind指的是绑定哪个ip可以访问 bind 要填写你自己r ...
- 正则简单操作cookie、url search
正则操作cookie.url getCookie function getCookie(key) { var cookies = window.document.cookie, reg = new R ...
- jquery中的 parseJSON() 源码分析
parseJSON: function( data ) { // Attempt to parse using the native JSON parser first if ( window.JSO ...
- Earth Wind 一个查看全球风向的网站
可以查看整个地球的全貌 ,还能定位你的位置,特别是动画挺有意思 网址:https://earth.nullschool.net/#current/wind/surface/level/orthogra ...
- React Native的学习资源网址
react官方文档(英文): https://facebook.github.io/react/docs/getting-started.html react中文社区(内部有视频教程等): htt ...
- 你使用的ie版本过低请。。。
放到body里面 <body> <!--[if lte IE 8]> <p class="chromeframe">您使用的IE浏览器版本过低. ...
- Huawei BGP和OSPF双边界重分布(一)
网络拓扑: PS:本例使用明细前缀列表双边界引入 S5700-LSW1 ================================================================ ...
- laravel -查询近7月走势图案例
// 获取7月前的时间$time = date('Y-m',strtotime("-0 year -7 month -0 day" ));$where['created_at'] ...
- IDEA配置
关于IDEA的配置 配置注释模板 CTRL_SHIFT_S,在Live Templates中新增一个TemplateGroup,然后再新建两个模板,如下图: 新增cc-ClassComment /** ...