Python 2 --> 3: object of type 'zip' has no len()

前端 未结 5 643
忘掉有多难
忘掉有多难 2020-12-13 17:38

I\'m following a tutorial on neural nets1

It\'s in Python 2.7. I\'m using 3.4. This is the line that troubles me:

if test_data: n_test = len(test_data)

5条回答
  •  误落风尘
    2020-12-13 18:08

    Some Info

    This is because in the Python 3.x, zip returns a generator object. This object is not a list (it's better) but it behaves like one. You can try iterating through it like this:

    for i in zip([1,2,3,4], ['a','b','c','d']):
        print i
    

    The Quick Answer

    Please show us the code surrounding where the error happened. But I think I can still give you a quick (and not necessarily good) solution.

    turn this

    for i in reversed(range(1, len(x))):
    

    into this:

    for i in reversed(range(1, len(list(x))):
    

提交回复
热议问题