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 attempted to translate it to py 3.x but I'm stuck on this section. Could anyone clarify what is wrong?
import random
emails = {
"x": "[REDACTED]@hotmail.com",
"x2": "[REDACTED]@hotmail.com",
"x3": "[REDACTED]@hotmail.com"
}
people = emails.keys()
#generate a number for everyone
allocations = range(len(people))
random.shuffle(allocations)
This was the error given TypeError: 'range' object does not support item assignment
In Python 3, range returns a lazy sequence object - it does not return a list. There is no way to rearrange elements in a range object, so it cannot be shuffled.
Convert it to a list before shuffling.
allocations = list(range(len(people)))
TypeError: 'range' object does not support item assignment的更多相关文章
- TypeError: 'range' object does not support item assignment处理方法
vectorsum.py#!/usr/bin/env/pythonimport sysfrom datetime import datetimeimport numpy as np # def num ...
- 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 其实 ...
随机推荐
- mysql case when 判断null
select name,case WHEN m.NAME is null THEN '' else m.NAME end NAME1 from sys_users
- MT【46】不动点,稳定点几何直观
评:不动点概念在数列的一类题中也是非常有用的.
- Hdoj 基本输入输出8道(1089-1096)
Hdoj 1089 #include<bits/stdc++.h> using namespace std; int main() { int a,b; while(cin>> ...
- BZOJ 3612: [Heoi2014]平衡
3612: [Heoi2014]平衡 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 283 Solved: 219[Submit][Status][ ...
- 安装workflowmanager 1.0 server
1· 下载安装 Web Platform Installer v4 Command Line (WebPICMD.exe) Tool.(http://download.microsoft.com/do ...
- 端午漫谈(附:Ubuntu18.04下轻量截图软件)
先说声端午快乐- 有空就陪陪家人吧.今天陪外公吃了顿饭,陪老人家聊了会天,颇有点感触.发现技术真的是改变生活,小孩抖音自学跳舞,大人微信刷又刷,很多天海一方的老朋友都可以联系到了... 其实最有感触的 ...
- 在kubernetes集群上用helm安装Datadog(Monitoring)
Datadog is a monitoring service that gathers monitoring data from your containers within your Azure ...
- Android开发属性动画
普通动画效果和属性动画效果区别: 普通动画效果的动画播放后只是产生了视觉欺骗,并没有移动真实的控件. 属性动画直接真实的移动控件 AnimationSet动画: TextView t1 = (Text ...
- poj 3061(二分 or 尺取法)
传送门:Problem 3061 https://www.cnblogs.com/violet-acmer/p/9793209.html 马上就要去上课了,先献上二分AC代码,其余的有空再补 题意: ...
- python 面试题--你能做出多少?
python3中__get__,getattr,__getattribute__的区别 什么是 GIL 详细博客 GIL = Global Intercept Lock 全局解释器锁,任意时刻在解释器 ...