【问题标题】:comparing strings and calculating occurrence比较字符串并计算出现次数
【发布时间】:2013-08-29 06:29:39
【问题描述】:

我不知道如何解决这个问题。

我有 3 个列表,其中包含一个单词、一个标签和一个出现在文档上的数字:

v1 = [['be', 'VSIS3S0', 1], ['scott', 'NP00000', 2], ['north', 'NCMS000', 1], ['revolution', 'NP00000', 1], ['name', 'VMP00SM', 1]]
v2 = [['mechanic', 'NCMS000', 1], ['be', 'VSIS3S0', 1], ['tool', 'AQ0CS0', 1], ['sam', 'NP00000', 1], ['frida', 'NP00000', 1]]
v3 = [['be', 'VSIP3S0', 1], ['scott', 'NP00000', 1], ['who', 'NP00000', 1]]

我如何构建一个函数来接收这些列表比较每个单词,例如,v1 中的单词 be 在三个列表中出现一次,在这种情况下附加到结果列表 (1 * log(3/3)),其中 1 -> 出现的最大值(这是子列表的第三个元素),对数分子 3 -> 常量,对数分母 3 -> 因为该词出现在 v1v2v3

接下来我们有scott -> 在这种情况下附加到结果列表(2 * log(3/2)),2 -> 出现的最大单词值,对数分子3 -> 常数,对数分母2 -> 因为单词' scott' 出现在 v1v2

接下来我们有north -> 在这种情况下追加到结果列表(1 * log(3/1)),1 -> 最大单词的出现值,对数分子3 -> 常数,对数分母1 -> 因为单词' north' 仅出现 v1

接下来我们有revolution -> 在这种情况下追加到结果列表(1 * log(3/1)),1 -> 最大单词的出现值,对数分子3 -> 常数,对数分母1 -> 因为单词' north' 仅出现 v1

接下来我们有name -> 在这种情况下追加到结果列表(1 * log(3/1)),1 -> 最大单词的出现值,对数分子3 -> 常数,对数分母1 -> 因为单词' name' 仅出现 v1

此外,我们还必须对v2 做同样的事情,将mechanicbetool 等与换句话说,计算出现的最大值并将其相乘w/ log(3/?),这取决于是否v1v3 中是否出现单词。

这是我对v1的尝试:

def f1(v1, v2, v3):
    res =[]
    for e in v1:
        if e != 0:
            if e in v2 and e in v3:
                res.append(0)
            elif e in v2:
                res.append(e * math.log(3/2))
            else:
                res.append(e * math.log(3))
    return res  

返回:[0, 2.1972245773362196, 0, 0, 0, 0]

这显然不是结果

它应该返回类似:

[['be', 0.47], ['scott', 0.35 ], ['north', 0.47], ['revolution', 0.47], ['north', 0.47]]

【问题讨论】:

    标签: python string list math


    【解决方案1】:

    根据你的描述,我得到了

    import math
    v1 = [['be', 'VSIS3S0', 1], ['scott', 'NP00000', 2], ['north', 'NCMS000', 1], ['revolution', 'NP00000', 1], ['name', 'VMP00SM', 1]]
    v2 = [['mechanic', 'NCMS000', 1], ['be', 'VSIS3S0', 1], ['tool', 'AQ0CS0', 1], ['sam', 'NP00000', 1], ['frida', 'NP00000', 1]]
    v3 = [['be', 'VSIP3S0', 1], ['scott', 'NP00000', 1], ['who', 'NP00000', 1]]
    
    v = [v1,v2,v3]
    
    countdict = {}
    for vi in v:
        for e in vi:
            countdict[e[0]] = countdict.get(e[0],0) + 1
    
    scoredict = {}
    for vi in v:
        for e in vi:
            scoredict[e[0]] = scoredict.get(e[0],0) + (e[2] * math.log10(3.0/countdict[e[0]]))
    
    print scoredict
    

    我将输出保存为dict,即:

    {'be': 0.0, 'revolution': 0.47712125471966244, 'north': 0.47712125471966244, 'name': 0.47712125471966244, 'sam': 0.47712125471966244, 'tool': 0.47712125471966244, 'who': 0.47712125471966244, 'scott': 0.5282737771670437, 'mechanic': 0.47712125471966244, 'frida': 0.47712125471966244}
    

    【讨论】:

    • 感谢大卫,这几乎是结果!!!一句话:python数学日志(3)抛出1.098,但普通计算器显示0.47......为什么?
    • 你需要什么日志库?
    • 我需要将 math.log(x) 更改为 math.log10(x)
    • @David.Zheng 我实现了你的解决方案并给了我: countdict[e[0]] = countdict.get(e[0],0) + 1 TypeError: 'int' object has no属性'getitem'
    • @JPP,捕获这样的异常很奇怪,这意味着 countdict 的类型在您的代码中是 int 。
    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 2014-04-24
    • 2019-08-23
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多