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

accepted

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的更多相关文章

  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. 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 ...

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

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

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

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

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

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

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

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

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

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

  8. python TypeError: 'str' object does not support item assignment”

    想替换string里的空格,遍历替换提示如题错误,查询得知string类型不可更改 import string s = "2013/2/12" b = s.replace('/', ...

  9. TypeError: '_io.TextIOWrapper' object does not support item assignment

    纯小白 遇到的细节问题: 报错 一开始看到这个傻逼了 TypeError: '_io.TextIOWrapper' object does not support item assignment 其实 ...

随机推荐

  1. 稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记。

    稍稍乱入的CNN,本文依然是学习周莫烦视频的笔记. 还有 google 在 udacity 上的 CNN 教程. CNN(Convolutional Neural Networks) 卷积神经网络简单 ...

  2. 【BZOJ2281】[SDOI2011]黑白棋(博弈论,动态规划)

    [BZOJ2281][SDOI2011]黑白棋(博弈论,动态规划) 题面 BZOJ 洛谷 题解 先看懂这题目在干什么. 首先BZOJ上面的题面没有图,换到洛谷看题就有图了. 不难发现都相邻的两个异色棋 ...

  3. js的==和===练习

    今天检查了一下JS的== 和===的区别 如下: 表达式                              值 1==true  true 1===true  false 0==false t ...

  4. luogu3707 相关分析 (线段树)

    把式子展开以后会发现,可以用线段树维护$x,y,x*y,x^2$分别的区间和 然后操作有区间加和区间修改 这个pushdown的时候,如果改和加的标记同时存在,那一定是先改再加,要不然加的标记已经被清 ...

  5. c++ 静态变量报错 undefined reference to static members

    c++中静态变量不但要在头文件中declare,还要在实现的cpp中declare.当然也可以赋个初始值. class foo { int _i; public: foo(int i) : _i(i) ...

  6. sort逆序(char String)

    import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class Main { ...

  7. ElasticStack系列之二十 & 数据均衡、迁移、冷热分离以及节点自动发现原理与机制

    1. 数据均衡 某个shard分配到哪个节点上,一般来说,是由 ELasticSearch 自行决定的.以下几种情况会触发分配动作: 新索引的建立 索引的删除 新增副本分片 节点增减引发的数据均衡 在 ...

  8. Cookie浅谈

    相信大家对Cookie多多少少都有点了解吧~~下面我来谈谈吧: 每个Cookie不超过4K数据,每个网站不超过20个Cookie,所有网站的Cookie总和不超过300个 虽然cookie已经是上世纪 ...

  9. js对象之window和document区别

    window是整个页面的全局环境,而document可以理解为整个页面这个最大的元素(整个dom树) window: 可以看到window下面有很多变量 document: 可见document是整个 ...

  10. C#常用的正则工具类写法

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