2017-02-14 53 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