[英]Python: Extract keywords from string
Hey Guys I am searching for a fast/efficient way to extract keywords (defined in a list) from a String (in a Dataframe) without being case sensitive or dependent on " " chars:嘿伙计们,我正在寻找一种快速/有效的方法来从字符串(在数据框中)中提取关键字(在列表中定义),而不区分大小写或依赖于“”字符:
keys = ['I', 'love', 'Cookies'] keys = ['我', '爱', '饼干']
String from df= "xxxxxxxxIxx xx cookies"来自 df= "xxxxxxxxIxx xx cookies" 的字符串
result should by either ['I'] or ['I', 'Cookies']结果应为 ['I'] 或 ['I', 'Cookies']
I am currently using f"({'|'.join(keys)}) which is case sensitive. What would you recommend for long strings in even longer dataframes:)我目前正在使用区分大小写的 f"({'|'.join(keys)}) 。对于更长的数据帧中的长字符串,您会推荐什么:)
Thanks in advance提前致谢
Working code as per your inputs:根据您的输入工作代码:
my_str ="xxxxxxxixxx xx cookhes"
my_list = ["I", "love", "Cookies"]
if any(substring.casefold() in my_str.casefold() for substring in my_list):
print('Contains element')
else:
print('Not contain any element.')
More info on the following answer from StackOverflow:Case insensitive 'in'有关 StackOverflow 的以下答案的更多信息:Case insensitive 'in'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.