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?
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.
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.
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.
Set objects are unordered and are therefore not subscriptable.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With