我已经预定了字符串触发列表的命令:
triggers = {'academic': ['studied at', 'studies at', 'studies', 'studies at'],
'age': ['years old','months old'],
'gender': ['male', 'female'],
'pets': ['dog','cat'],
'location': ['Lived in','Lives in']}
我有一个以前未知的分组信息列表列表,例如:
example_list_of_list = [['Former Teacher of math at'],
['Studies programming at', 'Stackoverflow'],
['Lives in','Chicago'],
['owns','dog', 'cat']
我想使用匹配预定义的键值将每个匹配列表元素附加到新字典上,例如:
{'academic': ['Former Teacher of math at'],
'age': None, # np.nan or []
'gender': None, # np.nan or []
'pets': ['owns','dog','cat']
'location': ['Lives in','Chicago']
}
谢谢!
您可以最轻松地使用集合语义来执行此操作:
result = {}
for input in example_list_of_list:
for key, triggerset in triggers.items():
if not input.isdisjoint(triggerset):
result[key] = result.get(key,[]).append(input)
尽管注意几件事:
-
triggers
应该是set
s的dict
,而不是list
s。 -
example_list_of_lists
应该是set
S的list
,而是 -
result
是list
s的list
的dict
,因为多个输入可能匹配