【发布时间】:2019-03-10 08:46:56
【问题描述】:
我的主字符串在数据框中,子字符串存储在列表中。我想要的输出是找到匹配的子字符串。这是我正在使用的代码。
sentence2 = "Previous study: 03/03/2018 (other hospital) Findings: Lung parenchyma: The study reveals evidence of apicoposterior segmentectomy of LUL showing soft tissue thickening adjacent surgical bed at LUL, possibly post operation."
blob_sentence = TextBlob(sentence2)
noun = blob_sentence.noun_phrases
df1 = pd.DataFrame(noun)
comorbidity_keywords = ["segmentectomy","lobectomy"]
matches =[]
for comorbidity_keywords[0] in df1:
if comorbidity_keywords[0] in df1 and comorbidity_keywords[0] not in matches:
matches.append(comorbidity_keywords)
这给了我作为不是实际匹配的字符串的结果。输出应该是“segmentectomy”。但我得到 [0,'lobectomy']。请帮忙!!。我试图从这里发布的答案中寻求帮助。 Check if multiple strings exist in another string请帮忙看看我做错了什么?
【问题讨论】:
-
从修复
for comorbidity_keywords[0] in df1:开始——您实际上是在迭代DataFrame,将每一行存储为comorbidity_keywords列表的第一个元素。将该行替换为for keyword in comorbidity_keywords:之类的内容,然后在您的if...检查中使用keyword而不是comorbidity_keywords[0]。 -
@zwer 像这样编辑 `matches =[] ` for keyword in comorbidity_keywords: if keyword in df1 and keyword not in matches: matches.append(keyword) 但这给出了空结果
标签: python string pandas textblob