<small id='8JIKc'></small><noframes id='8JIKc'>

  • <legend id='8JIKc'><style id='8JIKc'><dir id='8JIKc'><q id='8JIKc'></q></dir></style></legend>

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

        <bdo id='8JIKc'></bdo><ul id='8JIKc'></ul>

      1. <tfoot id='8JIKc'></tfoot>
      2. TypeError:“dict_keys"对象不支持索引

        时间:2024-04-20
      3. <small id='EpfC9'></small><noframes id='EpfC9'>

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

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

                  问题描述

                  def shuffle(self, x, random=None, int=int):"""x, random=random.random -> 随机播放列表 x;返回无.可选 arg random 是一个 0 参数函数,返回一个随机数浮动 [0.0, 1.0);默认情况下,标准 random.random."""randbelow = self._randbelowfor i in reversed(range(1, len(x))):# 在 x[:i+1] 中选择一个元素来交换 x[i]j = randbelow(i+1) if random 是 None else int(random() * (i+1))x[i], x[j] = x[j], x[i]

                  当我运行 shuffle 函数时,它会引发以下错误,这是为什么呢?

                  TypeError: 'dict_keys' 对象不支持索引

                  解决方案

                  很明显,您将 d.keys() 传递给您的 shuffle 函数.可能这是用 python2.x 编写的(当 d.keys() 返回一个列表时).在 python3.x 中,d.keys() 返回一个 dict_keys 对象,该对象的行为更像一个 set 而不是 list.因此,它无法被索引.

                  解决方案是将list(d.keys())(或简单的list(d))传递给shuffle.p>

                  def shuffle(self, x, random=None, int=int):
                      """x, random=random.random -> shuffle list x in place; return None.
                  
                      Optional arg random is a 0-argument function returning a random
                      float in [0.0, 1.0); by default, the standard random.random.
                      """
                  
                      randbelow = self._randbelow
                      for i in reversed(range(1, len(x))):
                          # pick an element in x[:i+1] with which to exchange x[i]
                          j = randbelow(i+1) if random is None else int(random() * (i+1))
                          x[i], x[j] = x[j], x[i]
                  

                  When I run the shuffle function it raises the following error, why is that?

                  TypeError: 'dict_keys' object does not support indexing
                  

                  解决方案

                  Clearly you're passing in d.keys() to your shuffle function. Probably this was written with python2.x (when d.keys() returned a list). With python3.x, d.keys() returns a dict_keys object which behaves a lot more like a set than a list. As such, it can't be indexed.

                  The solution is to pass list(d.keys()) (or simply list(d)) to shuffle.

                  这篇关于TypeError:“dict_keys"对象不支持索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python中的函数链 下一篇:SyntaxError:解析时出现意外的 EOF

                  相关文章

                    <bdo id='QJQPj'></bdo><ul id='QJQPj'></ul>

                • <tfoot id='QJQPj'></tfoot>

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

                  <legend id='QJQPj'><style id='QJQPj'><dir id='QJQPj'><q id='QJQPj'></q></dir></style></legend>

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