Python 使用"triggers strings"列表的字典从列表列表中输出新的标记字典



我已经预定了字符串触发列表的命令:

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,而是
  • resultlist s的listdict,因为多个输入可能匹配

最新更新