如何使用python为关键字分配权重

原文标题How to assign weights to keywords using python

有从其 HTML 标签分类的关键字,需要为标签分配权重,并分别获得每个关键字的计算总权重。

H1 tag keywords: ['jquery']
    
H2 tag keywords:['aws', 'jquery']

p tag keywords:['country', 'jquery', 'aws']

需要为标签分配权重(示例格式)

H1 - 10
H2 -  5
p  -  3

从关键字中获取计算的权重(只需要总数)

jquery  : 10 + 5 + 3 = 18
aws     : 5 + 3 = 8
country : 3 = 3

原文链接:https://stackoverflow.com//questions/71555290/how-to-assign-weights-to-keywords-using-python

回复

我来回复
  • CarlosSR的头像
    CarlosSR 评论

    有很多不同的方法可以解决这个问题,虽然这不是最优雅的一种,但如果我们假设您有不同的 h1、h2 和 p 列表,您可以执行以下操作:

    weightsdict = { 'h1': 10, 'h2': 5, 'p':3 }
    stringtoeval = ['jquery', 'aws', 'country']
    
    for s in stringtoeval:
    print(s, weightsdict[h1]*h1list.count(s)+weightsdict[h1]*h2list.count(s)+weightsdict[h1]*plist.count(s))
    
    
    2年前 0条评论