2017-02-14 89 views
1

我仍在學習python,嘗試一些東西。從列表中匹配

我有代碼在文本中搜索單詞'Lorem'並用列表中的隨機單詞替換。這是行得通的。

我現在要做的是如何檢查列表中的單詞(單詞= ['和','a','是',''])是否在文本中,並用另一個單詞中的另一個單詞替換(t = ['TEXT','REPLACE','WORD'])。

我想用變量或循環直通列表替換'Lorem'或用單詞打開txt文件來檢查。

解析度=應用re.sub( '的Lorem',拉姆達X:random.choice(T),文本)

如果可能的話,如果有人可以告訴我所有的3個選項:

-loop通過列表 - 變量 - 用文字打開內部文件

或者也許還有其他更好的方法?

謝謝!


這裏是完整的代碼


import re 
import random 

t = ['TEXT', 'REPLACE', 'WORD'] 

text = '''Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum''' 

words = ['and', 'a', 'is', 'the'] 

res = re.sub('Lorem', lambda x: random.choice(t), text) 

print(res) 
+0

如何'words'對應't'?我們是否假設你只是隨機地用't'中的一個單詞替換?如果沒有,我建議你創建一個字典映射哪些單詞應該被某些其他單詞替換。 – blacksite

回答

0

下面的代碼將在text,這也是在wordsreplacement一個隨機單詞替換任何字。

import random 
replacement = ['TEXT', 'REPLACE', 'WORD', 'LIST'] 

text = '''Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum''' 
# with open('ipsum.txt') as tf: text = tf.read(None) 

words = ['and', 'a', 'is', 'the'] 

text_words = text.split() 

for ti, tw in enumerate(text_words): 
    if tw in words: 
     text_words[ti] = random.choice(replacement) 

print(' '.join(text_words)) 

# Possible output: 
# Lorem Ipsum WORD simply dummy text of LIST printing REPLACE typesetting industry. Lorem Ipsum has been REPLACE industry's standard dummy text ever since TEXT 1500s, when an unknown printer took WORD galley of type LIST scrambled it to make WORD type specimen book. It has survived not only five centuries, but also TEXT leap into electronic typesetting, remaining essentially unchanged. It was popularised in WORD 1960s with REPLACE release of Letraset sheets containing Lorem Ipsum passages, REPLACE more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum 

如果你想從replacement採取同樣的指數,如words,你可以用這個循環:

for ti, tw in enumerate(text_words): 
    try: 
     wi = words.index(tw) 
    except ValueError: 
     pass 
    else: 
     text_words[ti] = replacement[wi] 

print(' '.join(text_words)) 

# Result: 
# Lorem Ipsum WORD simply dummy text of LIST printing TEXT typesetting industry. Lorem Ipsum has been LIST industry's standard dummy text ever 
+0

令人驚歎,效果很棒!謝謝! – sLOVEnia

+0

@sLOVEnia,如果它解決了你的問題,請標記爲解決方案。謝謝! – amotzg