我有一个由词语及其标签组成的词性对的列表,我使用以下函数迭代这个列表并选择符合if语句但我有错误的对。这是清单:
[[('After', 'IN'), ('generating', 'VBG')],
[('generating', 'VBG'), ('all', 'DT')],
[('all', 'DT'), ('feature', 'NN')],
[('feature', 'NN'), ('spaces', 'NNS')]]
这是功能:
def select_posesPairs(tweet):
D=[]
s = getTokenss(tweet)
for first,second in s:
for word,tag in second:
if tag =='NN':
D.append(word)
return D
这就是错误:
ValueError: too many values to unpack
答案 0 :(得分:0)
你有一个嵌套列表。并且第一次迭代在外部列表上
你可以做到
for sub_list in s:
for tuple_data in sub_list:
if tuple_data[1] == 'NN'
结果
In [3]: s = [[('After', 'IN'), ('generating', 'VBG')],
...: [('generating', 'VBG'), ('all', 'DT')],
...: [('all', 'DT'), ('feature', 'NN')],
...: [('feature', 'NN'), ('spaces', 'NNS')]]
In [4]: for sub_list in s:
...: for tup in sub_list:
...: print(tup[1])
...:
IN
VBG
VBG
DT
DT
NN
NN
NNS