sss*_*sss 60 python python-3.x
我有格式为token/tag的标记文件,我尝试了一个函数,它返回一个带有来自(word,tag)列表的单词的元组.
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
Run Code Online (Sandbox Code Playgroud)
在python 2.7中它运行良好,但在python 3.4中它给我以下错误:
return " ".join(list[zip(*ngram)[0]])
TypeError: 'zip' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
khe*_*ood 101
在Python 2中,zip
返回了一个列表.在Python 3中,zip
返回一个可迭代对象.但是你可以通过调用将它变成一个列表list
.
那将是:
list(zip(...))
Run Code Online (Sandbox Code Playgroud)
这就是一个列表,就像在Python 2中一样,所以你可以使用:
list(zip(*ngram))
Run Code Online (Sandbox Code Playgroud)
等等
但是如果你只需要第一个元素,那么你就不需要一个列表.你可以使用next
.
那将是:
list(zip(*ngram))[0]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
44946 次 |
最近记录: |