我正在研究项目中的问题。 我有一个带有列的数据库,我存储了类型,如“15分钟休息”或“30分钟空闲时间” 我想用类别创建另一个列。 我的类别存储在字典中:
{ "short":["10","5","15","10min","5min","15min","shorter"],
"middle":["20","25","30","35","20min","25min","30min","35min"],
"long":["40","45","50","55","60","40min","45min","50min","55min","60min"]}
任何想法如何使用python为类型分配类别? 我的意思是那部分与字典中的单词相似? 我的代码到目前为止
...calling sql select
for i, index in rows():
type = index[0]
if (any of words from dictionary) is in type:
category = (name of category, for example "short")
update in sql
...
THx的
答案 0 :(得分:0)
您想知道任何类别标记是否在中断描述中。假设s1
和s2
是您的示例说明,d
是您的字典:
s1 = "15 mins break"
s2 = "30 min free time"
s3 = "something5something"
然后,下面的表达式将评估它们的类别(re.findall()
的目的是标记文本;将正则表达式替换为适合您项目的任何类型。)
[cat for cat in d if any(marker in re.findall(r'[a-z0-9]+',s1) for marker in d[cat])]
#['short']
[cat for cat in d if any(marker in re.findall(r'[a-z0-9]+',s2) for marker in d[cat])]
#['middle']
[cat for cat in d if any(marker in re.findall(r'[a-z0-9]+',s3) for marker in d[cat])]
#[]
这假设所有字符串都是小写字母。