<i id='QFa5k'><tr id='QFa5k'><dt id='QFa5k'><q id='QFa5k'><span id='QFa5k'><b id='QFa5k'><form id='QFa5k'><ins id='QFa5k'></ins><ul id='QFa5k'></ul><sub id='QFa5k'></sub></form><legend id='QFa5k'></legend><bdo id='QFa5k'><pre id='QFa5k'><center id='QFa5k'></center></pre></bdo></b><th id='QFa5k'></th></span></q></dt></tr></i><div id='QFa5k'><tfoot id='QFa5k'></tfoot><dl id='QFa5k'><fieldset id='QFa5k'></fieldset></dl></div>
      <bdo id='QFa5k'></bdo><ul id='QFa5k'></ul>

    1. <legend id='QFa5k'><style id='QFa5k'><dir id='QFa5k'><q id='QFa5k'></q></dir></style></legend><tfoot id='QFa5k'></tfoot>

    2. <small id='QFa5k'></small><noframes id='QFa5k'>

        TypeError:“范围"对象不支持项目分配

        TypeError: #39;range#39; object does not support item assignment(TypeError:“范围对象不支持项目分配)
            <tbody id='idFX4'></tbody>

        1. <legend id='idFX4'><style id='idFX4'><dir id='idFX4'><q id='idFX4'></q></dir></style></legend>

          <small id='idFX4'></small><noframes id='idFX4'>

          <i id='idFX4'><tr id='idFX4'><dt id='idFX4'><q id='idFX4'><span id='idFX4'><b id='idFX4'><form id='idFX4'><ins id='idFX4'></ins><ul id='idFX4'></ul><sub id='idFX4'></sub></form><legend id='idFX4'></legend><bdo id='idFX4'><pre id='idFX4'><center id='idFX4'></center></pre></bdo></b><th id='idFX4'></th></span></q></dt></tr></i><div id='idFX4'><tfoot id='idFX4'></tfoot><dl id='idFX4'><fieldset id='idFX4'></fieldset></dl></div>

            • <bdo id='idFX4'></bdo><ul id='idFX4'></ul>
                <tfoot id='idFX4'></tfoot>

                  本文介绍了TypeError:“范围"对象不支持项目分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在查看一些 python 2.x 代码并试图将其转换为 py 3.x 但我被困在这一部分.谁能澄清什么是错的?

                  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)
                  

                  这是给出的错误:

                  TypeError: 'range' object does not support item assignment
                  

                  推荐答案

                  在 Python 3 中,range 返回一个惰性序列对象——它不返回一个列表.无法重新排列范围对象中的元素,因此无法对其进行混洗.

                  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:“范围"对象不支持项目分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  • <tfoot id='cA6rG'></tfoot>
                      <tbody id='cA6rG'></tbody>
                  • <legend id='cA6rG'><style id='cA6rG'><dir id='cA6rG'><q id='cA6rG'></q></dir></style></legend>

                      1. <i id='cA6rG'><tr id='cA6rG'><dt id='cA6rG'><q id='cA6rG'><span id='cA6rG'><b id='cA6rG'><form id='cA6rG'><ins id='cA6rG'></ins><ul id='cA6rG'></ul><sub id='cA6rG'></sub></form><legend id='cA6rG'></legend><bdo id='cA6rG'><pre id='cA6rG'><center id='cA6rG'></center></pre></bdo></b><th id='cA6rG'></th></span></q></dt></tr></i><div id='cA6rG'><tfoot id='cA6rG'></tfoot><dl id='cA6rG'><fieldset id='cA6rG'></fieldset></dl></div>
                          <bdo id='cA6rG'></bdo><ul id='cA6rG'></ul>
                          • <small id='cA6rG'></small><noframes id='cA6rG'>