2011-03-31 83 views
0
pos_tag(word_tokenize("John's big idea isn't all that bad.")) 
[('John', 'NNP'), ("'s", 'POS'), ('big', 'JJ'), ('idea', 'NN'), ('is', 
'VBZ'), ("n't", 'RB'), ('all', 'DT'), ('that', 'DT'), ('bad', 'JJ'), 
('.', '.')] 

根本不識別語法。我將如何重複檢查JJ的第二個值對。輸出是什麼數據類型?

回答

6

它看起來像對的列表(大小爲2的元組)。

迭代很簡單:

for text, type in pos_tag(word_tokenize("John's big idea isn't all that bad.")): 
    if type == 'JJ': 
     print 'text:', text 
     print 'type:', type 
1

看起來像一個2元組列表給我。

[x for x in L if x[1] == 'JJ'] 
1
list_values = [ 
    ('John', 'NNP'), 
    ("'s", 'POS'), 
    ('big', 'JJ'), 
    ('idea', 'NN'), 
    ('is', 'VBZ'), 
    ("n't", 'RB'), 
    ('all', 'DT'), 
    ('that', 'DT'), 
    ('bad', 'JJ'), 
    ('.', '.') 
] 
for (a, b) in list_values: 
    if b == 'JJ': 
     DoSomething(a,b)