2011-07-31 94 views
2

我有一個列表中包含大量單詞: sentence = ['a','list','with','a','lot','of' ,'strings','in','it']將字符串連接到列表中的下一個字符串

我希望能夠通過列表並根據一些條件合併單詞對。例如

['a','list','with','a','lot','of','strings','in','it'] 變成 ['a list', 「與」,「很多」,「中」,「弦」,「中」,「它」]

我已經試過類似:

for w in range(len(sentence)): 
    if sentence[w] == 'a': 
     sentence[w:w+2]=[' '.join(sentence[w:w+2])] 

,但它並沒有因爲加入工作字符串會減小列表的大小並導致索引超出範圍。有沒有辦法做到這一點與迭代器和.next()什麼的?

+0

就地一requi正在修改'sentence'或者是否有足夠的記憶來保存(至少暫時)第二個「句子」副本? – unutbu

+0

我想這樣做,因爲我的列表中有超過一百萬個單詞。 –

回答

0

像這樣的東西?

#!/usr/bin/env python 

def joiner(s, token): 
    i = 0 
    while i < len(s): 
     if s[i] == token: 
      yield s[i] + ' ' + s[i+1] 
      i=i+2 
     else: 
      yield s[i] 
      i=i+1 

sentence = ['a','list','with','a','lot','of','strings','in','it'] 

for i in joiner(sentence, 'a'): 
    print i 

輸出:

a list 
with 
a lot 
of 
strings 
in 
it 
+0

這就是我最終使用的。謝謝。 –

0

您可以使用while週期並手動增加索引w

0

天真的方法:

#!/usr/bin/env python 

words = ['a','list','with','a','lot','of','strings','in','it'] 

condensed, skip = [], False 

for i, word in enumerate(words): 
    if skip: 
     skip = False 
     continue 
    if word == 'a': 
     condensed.append(word + " " + words[i + 1]) 
     skip = True 
    else: 
     condensed.append(word) 

print condensed 
# => ['a list', 'with', 'a lot', 'of', 'strings', 'in', 'it'] 
4

您可以使用一個迭代器。

>>> it = iter(['a','list','with','a','lot','of','strings','in','it']) 
>>> [i if i != 'a' else i+' '+next(it) for i in it] 
['a list', 'with', 'a lot', 'of', 'strings', 'in', 'it'] 
+0

這看起來非常優雅,但我不太明白髮生了什麼。我將如何添加多個加入條件?例如:如果我!='a'和next(it)!='lot' –

+0

'next(it)'函數會遞增迭代器,所以'for'循環將不會使用該值兩次。 – JBernardo

+0

如果您有多個條件,您可能必須將'next(it)'保存到一個變量,然後檢查。你將無法使用理解,但一個簡單的循環應該工作。 – JBernardo

0
def grouped(sentence): 
    have_a = False 
    for word in sentence: 
     if have_a: 
      yield 'a ' + word 
      have_a = False 
     elif word == 'a': have_a = True 
     else: yield word 

sentence = list(grouped(sentence)) 
1

此作品就地:

sentence = ['a','list','with','a','lot','of','strings','in','it'] 

idx=0 
seen=False 
for word in sentence: 
    if word=='a': 
     seen=True 
     continue 
    sentence[idx]='a '+word if seen else word 
    seen=False 
    idx+=1  
sentence=sentence[:idx] 
print(sentence) 

產量

['a list', 'with', 'a lot', 'of', 'strings', 'in', 'it'] 
相關問題