我正在尝试使用XXX之类的占位符替换变量。单词“ hello”和“ morning”按原样打印,因为它们出现在另一个列表中。以下代码有效,但会打印出额外的占位符。
import re
mylist = ['hello', 'morning']
nm = [
"Hello World Robot Morning.",
"Hello Aniket Fine Morning.",
"Hello Paresh Good and bad Morning.",
]
def punctuations(string):
pattern = re.compile(r"(?u)\b\w\w+\b")
result = pattern.match(string)
myword = result.group()
return myword
for x in nm:
newlist = list()
for y in x.split():
for z in mylist:
if z.lower() == punctuations(y.lower()):
newlist.append(y)
else:
newlist.append("xxx")
print(newlist)
输出:
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
预期输出:
['Hello', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'Morning.']
['Hello', 'xxx', 'xxx', 'xxx', 'xxx', 'Morning.']
答案 0 :(得分:3)
实际上,使用进行正式解析可以更好地解决您的问题时,您就可以使用python的香草 string函数和正则表达式解析表达语法(PEP):
例如:
import pyparsing as pp
expr = pp.OneOrMore(pp.Word("hello") | pp.Word("world") | pp.Word(pp.alphas).setParseAction(pp.replaceWith("XXX")))
expr.parseString("hello foo bar world")
收益:
(['hello', 'XXX', 'XXX', 'world'], {})
答案 1 :(得分:2)
找到单词后,您必须休息一下,只有在检查ValueError: Image size of 216000x256000 pixels is too large. It must be less than 2^16 in each direction.
中的所有元素之后,才能评估是否找到了某些内容,如果没有,请附加占位符
my_list