2014-05-17 46 views
1

我是python的新手。我希望你能幫助我;)如何停止for循環並使其進一步工作?

我有一個數據:

[('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'), 
    ('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of', 
    'IN'), 
    ('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN'), ('.', '.'), 
    ('The','DT'), 
    ('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')......] 

,我想在列表中的列表,包含每一個句子,所以它看起來像:

[ [('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'), 
    ('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of', 
    'IN'), 
    ('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN')], [('The', 'DT'), 
    ('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')] ...] 

但我不知道該怎麼做:(我有一個幫助for循環

for el in data: 
    if el[0] != ('.' or '?' or '!'): # finds only points((
    sentences.append(el) 

試了一下我怎樣才能當他發現一個點時停止循環?我怎樣才能讓它看起來更進一步,並寫入一個新的列表?

+1

我真的不知道你在找什麼。也許你在問什麼條件可以用來檢查一個值與多個值的關係:如果是這樣,使用'in'就像這樣'if ['。','?','!']:中的el [0]。也許你在問如何在列表中添加一個列表:'a = [1,2,3]; a = [a]' – bozdoz

+0

標點符號的* second *元素是* always *'.'。 –

+0

我在一個列表中包含句子的數據,每個單詞的句子都是POS標記的。我想把每個句子都放在一個單獨的列表中。所以我想要一個大的列表,它的每個元素都是一個包含一個句子的列表(在句子結尾沒有標點符號)。對不起我的英文;( – Irina

回答

2
sentences = [] 
sentence = [] 

for word, code in data: 
    sentence.append((word, code)) 
    if word in '.?!': 
     sentences.append(sentence) 
     sentence = [] 

如果你不想點到包括在句子:

sentences = [] 
sentence = [] 

for word, code in data: 
    if word in '.?!': 
     sentences.append(sentence) 
     sentence = [] 
    else: 
     sentence.append((word, code)) 
+0

很好用!但我不想在列表中有一個點;(我該怎麼辦?) – Irina

+0

@Irina,看到更新 – warvariuc

0

您可以使用一臺發電機:

def per_sentence(qualified): 
    sentence = [] 
    for word, class_ in qualified: 
     sentence.append((word, class_)) 
     if class_ == '.': 
      yield sentence 
      sentence = [] 
    if sentence: 
     # yield tail 
     yield sentence 

然後產生與list()列表:

sentences = list(per_sentence(data)) 

演示:

>>> data = [('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'), 
... ('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of', 
... 'IN'), 
... ('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN'), ('.', '.'), 
... ('The','DT'), 
... ('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')] 
>>> def per_sentence(qualified): 
...  sentence = [] 
...  for word, class_ in qualified: 
...   sentence.append((word, class_)) 
...   if class_ == '.': 
...    yield sentence 
...    sentence = [] 
...  if sentence: 
...   # yield tail 
...   yield sentence 
... 
>>> list(per_sentence(data)) 
[[('Senators', 'NNS'), ('and', 'CC'), ('a', 'DT'), ('good', 'JJ'), ('teacher', 'NN'), ('believes', 'VBZ'), ('in', 'IN'), ('the', 'DT'), ('possibilities', 'NNS'), ('of', 'IN'), ('every', 'DT'), ('boy', 'NN'), ('and', 'CC'), ('girl', 'NN'), ('.', '.')], [('The', 'DT'), ('good', 'JJ'), ('teacher', 'NN'), ('sees', 'VBZ'), ('what', 'WP')]] 
+0

這是完美的!但我不想要在列表中籤名「!!」( – Irina

0

假設,在data每個元組的第二個元素是這個詞的類,並將於.所有句子終結,你可以試試:

sentences = [[],] 
for word in data: 
    sentences[-1].append(word) 
    if word[1] == '.': 
     sentences.append([]) 

芹苴,這將導致sentences如果最後的句子由.-class元素正確終止,則在末尾包含空列表。

+0

非常有用!!謝謝! – Irina