Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'zip' object is not subscriptable

I have a tagged file in the format token/tag and I try a function that returns a tuple with words from a (word,tag) list.

def text_from_tagged_ngram(ngram):      if type(ngram) == tuple:         return ngram[0]     return " ".join(zip(*ngram)[0]) # zip(*ngram)[0] returns a tuple with words from a (word,tag) list 

In python 2.7 it worked well, but in python 3.4 it gives me the following error:

return " ".join(list[zip(*ngram)[0]]) TypeError: 'zip' object is not subscriptable 

Can someone help?

like image 890
sss Avatar asked Dec 11 '14 20:12

sss


People also ask

How do you fix an object is not Subscriptable error?

The TypeError: 'int' object is not subscriptable error occurs if we try to index or slice the integer as if it is a subscriptable object like list, dict, or string objects. The issue can be resolved by removing any indexing or slicing to access the values of the integer object.

What is a zip object Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

What is a Subscriptable object?

In simple terms, a subscriptable object in Python is an object that can contain other objects, i.e., the objects which are containers can be termed as subscriptable objects. Strings , tuples , lists and dictionaries are examples of subscriptable objects in Python.

Is set Subscriptable?

Set objects are unordered and are therefore not subscriptable.


Video Answer


1 Answers

In Python 2, zip returned a list. In Python 3, zip returns an iterable object. But you can make it into a list just by calling list, as in:

list(zip(...)) 

In this case, that would be:

list(zip(*ngram)) 

With a list, you can use indexing:

items = list(zip(*ngram)) ... items[0] 

etc.

But if you only need the first element, then you don't strictly need a list. You could just use next.

In this case, that would be:

next(zip(*ngram)) 
like image 122
khelwood Avatar answered Oct 09 '22 03:10

khelwood