我有一张单子
List1 = ['Cappuccino','Café Latte','Expresso','Macchiato ','Irish coffee ']
我必须从用户处获取输入句子,如果列表1中的任何单词匹配,则应返回该列表1中的字符串以及一些图例
示例:输入输入字符串:
用户输入:I want 1 Cappuccino.
预期输出:item : Cappuccino
我的代码:
import pandas as pd
import re
def ccd():
List1 = ['Cappuccino','Café Latte','Expresso','Macchiato ','Irish coffee '],
for i in range(len(List1)):
List1[i] = List1[i].upper()
txt = input('Enter a substring: ').upper()
words = txt
matches = []
sentences = re.split(r'\.', txt)
keyword = List1[0]
pattern = keyword
re.compile(pattern)
for sentence in sentences:
if re.search(pattern, sentence):
matches.append(sentence)
print("Sentence matching the word (" + keyword + "):")
for match in matches:
print (match)
我建议根据关键字列表构建一个regex替换:
输出:
您不需要正则表达式:
示例输出:
相关问题 更多 >
编程相关推荐