Python 从列表中匹配

Python 从列表中匹配,python,python-3.x,Python,Python 3.x,我还在学习python,尝试一些东西 我有一段代码,可以在文本中搜索单词“Lorem”,并替换为列表中的随机单词。这是有效的 我现在想做的是如何检查列表中的任何单词(单词=['and','a','is','the'])是否在文本中,并替换为另一个列表中的另一个单词(t=['text','replace','word']) 我想用变量或循环列表替换'Lorem',或者用要检查的单词打开txt文件 res=re.sub('Lorem',lambda x:random.choice(t),text)

我还在学习python,尝试一些东西

我有一段代码,可以在文本中搜索单词“Lorem”,并替换为列表中的随机单词。这是有效的

我现在想做的是如何检查列表中的任何单词(单词=['and','a','is','the'])是否在文本中,并替换为另一个列表中的另一个单词(t=['text','replace','word'])

我想用变量或循环列表替换'Lorem',或者用要检查的单词打开txt文件

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

如果可能,如果有人可以向我展示所有3个选项:

-循环列表 -变数 -打开包含单词的文件

或者有其他更好的方法

谢谢


这是完整的代码



以下代码将用
替换
中的随机单词替换
文本
中也在
单词
中的任何单词

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
如果您想从
替换
中获取与
单词
相同的索引,可以使用以下循环:

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

以下代码将用
替换
中的随机单词替换
文本
中也在
单词
中的任何单词

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
如果您想从
替换
中获取与
单词
相同的索引,可以使用以下循环:

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

单词
如何对应于
t
?我们是否假设您只是随机替换为
t
中的一个单词?如果没有,我建议您创建一个字典映射哪些单词应该被某些其他单词替换。
单词
如何对应
t
?我们是否假设您只是随机替换为
t
中的一个单词?如果没有,我建议您创建一个字典映射,哪些单词应该替换为某些其他单词。@斯洛文尼亚,如果它解决了您的问题,请标记为解决方案。谢谢@斯洛文尼亚,如果它解决了您的问题,请标记为解决方案。谢谢!