2017-02-14 7 views
1

Ich lerne immer noch Python, Dinge versuchen.Treffer von der Liste

Ich habe Code, der nach dem Wort 'Lorem' im Text suchen und durch ein zufälliges Wort aus der Liste ersetzen. Das funktioniert.

Was ich jetzt tun möchte ist, wie man überprüft, ob ein Wort aus der Liste (Wörter = ['und', 'a', 'ist', 'das']) im Text ist und durch ein anderes Wort aus einer anderen Liste ersetzt (t = ['TEXT', 'ERSETZEN', 'WORT']).

Ich möchte "Lorem" durch Variable oder Schleife durch Liste ersetzen oder txt-Datei mit zu überprüfenden Wörtern öffnen.

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

Wenn möglich, wenn jemand alle drei Optionen zeigen mir kann:

-loop durch Liste -variable -öffnen Datei mit Wörtern innerhalb

Oder vielleicht gibt es andere, bessere Art und Weise?

Danke!


hier ist voll Code


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

Wie 'words' zu' t' entsprechen? Sollen wir annehmen, dass Sie nur zufällig eines der Wörter von "t" ersetzen? Wenn nicht, würde ich vorschlagen, dass Sie eine Wörterbuchzuordnung erstellen, welche Wörter durch bestimmte andere Wörter ersetzt werden sollten. – blacksite

Antwort

0

Der folgende Code Ein Wort in text ersetzen, die auch mit einem zufälligen Wort von replacement in words ist.

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 

Wenn Sie von replacement auf dem gleichen Index nehmen wollen, wie in words, können Sie diese Schleife verwenden:

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

Erstaunlich, funktioniert super! Vielen Dank! – sLOVEnia

+0

@sLOVEnia, wenn es Ihre Frage löst, markieren Sie bitte als Lösung. Danke! – amotzg

Verwandte Themen