考虑该草案文本段落:
Lorem ipsum dolor坐下来,精神上的精神。 Phasellus iaculis imperdiet hendrerit。 Aliquam vitae eleifend ex。 Aenean molestie mi vel mauris blandit sagittis。 Cras dictum ac nibh nec malesuada。 Fusce dolor ex,vulputate id ipsum eget,varius condimentum lectus。 Fusce accumsan nisl eget tincidunt elementum。 Suspendisse eu pellentesque odio。
如果包含单词('Lorem'AND'ipsum')或'dolor'
,我想检查此文本我如何在Python中执行此操作?
用例: 查询将保存到数据库中,因此,用户可以在管理区域中删除或添加或编辑查询。
答案 0 :(得分:0)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus iaculis imperdiet hendrerit. Aliquam vitae eleifend ex. Aenean molestie mi vel mauris blandit sagittis. Cras dictum ac nibh nec malesuada. Fusce dolor ex, vulputate id ipsum eget, varius condimentum lectus. Fusce accumsan nisl eget tincidunt elementum. Suspendisse eu pellentesque odio."
result = ('Lorem' in text and 'ipsum' in text) or 'dolor' in text
print(result)
输出:
True
答案 1 :(得分:0)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus iaculis imperdiet hendrerit. Aliquam vitae eleifend ex. Aenean molestie mi vel mauris blandit sagittis. Cras dictum ac nibh nec malesuada. Fusce dolor ex, vulputate id ipsum eget, varius condimentum lectus. Fusce accumsan nisl eget tincidunt elementum. Suspendisse eu pellentesque odio."
word_lists = [['Lorem', 'ipsum'], ['dolor']]
result = any(all(w in text for w in word_list) for word_list in word_lists)
print result
真
如果您需要添加更多条件,可以向word_lists添加元素。
答案 2 :(得分:0)
@Lhassan Baazzi
import re
r = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus iaculis imperdiet hendrerit. Aliquam vitae eleifend ex. Aenean molestie mi vel mauris blandit sagittis. Cras dictum ac nibh nec malesuada. Fusce dolor ex, vulputate id ipsum eget, varius condimentum lectus. Fusce accumsan nisl eget tincidunt elementum. Suspendisse eu pellentesque odio."
if re.search('Lorem' and 'ipsum',r) or re.search('dolor',r):
print 'Exists'