在python中压缩的LEN错误 [英] LEN error for zipping in python

查看:28
本文介绍了在python中压缩的LEN错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def shufflemode():
    import random
    combined = zip(question, answer)
    random.shuffle(combined)
    question[:], answer[:] = zip(*combined)

但后来我得到了错误:类型错误:zip"类型的对象没有 len()

but then i get the error: TypeError: object of type 'zip' has no len()

我怎么办我好迷茫

推荐答案

这是 Python 2 和 Python 3 之间的问题.在 Python 2 中使用 shuffle after zip 工作,因为 zip 返回一个列表.在 Python 3 中:TypeError: object of type 'zip' has no len()"因为 zip 在 Python 3 中返回一个迭代器.

This is an issue between Python 2 and Python 3. In Python 2 using shuffle after zip works, because zip returns a list. In Python 3: "TypeError: object of type 'zip' has no len()" because zip returns an iterator in Python 3.

解决方法,使用list()转换为列表:

Solution, use list() to convert to a list:

combined = list(zip(question, answer))
random.shuffle(combined)

shuffle() 出现错误是因为 shuffle() 使用了 len().

The error appeared with shuffle() because shuffle() uses len().

参考问题:python 3 中的 zip() 函数

这篇关于在python中压缩的LEN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆