TypeError: 'dict_keys' object does not support indexing

后端 未结 5 1409
Happy的楠姐
Happy的楠姐 2020-11-27 13:21
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 fu         


        
5条回答
  •  庸人自扰
    2020-11-27 14:09

    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.

提交回复
热议问题