【问题标题】:Creating a dictionary with calculated values创建具有计算值的字典
【发布时间】:2015-10-17 04:40:02
【问题描述】:

我有一个大文本字符串,我想创建一个字典,其中键 = 字符串中的一对单词(必须经过所有可能的组合),值 = 给定单词对的频率。因此,它是一个 2D 矩阵,每个矩阵元素都是一个数字(来自相互交叉的列和行的对的频率。单词在对中的位置无关紧要:例如,如果 Ridebike = 4(频率)然后骑自行车 = 4 以及

最终结果是填充矩阵,然后选择 N 个顶对。

我是使用文本字符串和 Python 的新手,我迷路了(我的“代码”中的循环也太多了)

这就是我所拥有的(删除停用词和标点符号后):

textNP = 'stopped traffic bklyn  bqe  278 wb manhattan brtillary stx29  wb  cadman pla  hope  oufootball makes safe manhattan kansas tomorrow  boomersooner  beatwildcats  theyhateuscuztheyaintus  hatersgonnahate rt  bringonthecats  bring cats exclusive  live footage oklahoma trying get manhattan  http  colktsoyzvvz rt  jonfmorse  bring cats exclusive  live footage oklahoma trying get manhattan'

一些代码(不完整和错误):

txtU = set(textNP)
lntxt = len(textNP)
lntxtS = len(txtU)

matrixNP = {}

for b1, i1 in txtU: 
    for b2, i2 in txtU:
        if i1< i2:
            bb1 = b1+b2
            bb2 = b2+b1

            freq = 0

            for k in textNP:
                for j in textNP:
                    if k < j:

                        kj = k+j
                        if kj == bb1 | kj == bb2:

                            freq +=1

            matrixNP[i1][i2] = freq
            matrixNP[i2][i1] = freq

        elif i1 == i2: matrixNP[i1][i1] = 1

我确信有许多循环是错误的问题之一。另外,我不确定如何将计算出的键(单词连接)分配给字典(我想我得到了正确的值)

文本字符串不是一个完整的产品:它将使用各种正则表达式从数字和其他一些东西中清除

非常感谢您的帮助!

【问题讨论】:

    标签: python dictionary matrix counter


    【解决方案1】:

    您是否正在寻找 2 个单词的所有组合,如果是,您可以使用 itertools.combinationscollections.Counter 来做您想做的事:

    >>> from itertools import combinations
    >>> from collections import Counter
    >>> N = 5
    >>> c = Counter(tuple(sorted(a)) for a in combinations(textNP.split(), 2))
    >>> c.most_common(N)
    [(('manhattan', 'rt'), 8),
     (('exclusive', 'manhattan'), 8),
     (('footage', 'manhattan'), 8),
     (('manhattan', 'oklahoma'), 8),
     (('bring', 'manhattan'), 8)]
    

    或者你是在寻找所有连续的单词对然后你可以创建一个成对函数:

    >>> from itertools import tee
    >>> from collections import Counter
    >>> def pairwise(iterable):
    ...     a, b = tee(iterable)
    ...     next(b, None)
    ...     return zip(a, b)    # itertools.izip() in python2
    >>> N = 5
    >>> c = Counter(tuple(sorted(a)) for a in pairwise(textNP.split()))
    >>> c.most_common(N)
    [(('get', 'manhattan'), 2),
     (('footage', 'live'), 2),
     (('get', 'trying'), 2),
     (('bring', 'cats'), 2),
     (('exclusive', 'live'), 2)]
    

    无论哪种方式,我都没有在列表中看到骑自行车。

    【讨论】:

    • repeat = 2 是什么意思,如果你不介意我问的话?你是说我不必创建矩阵?
    • repeat=2 获取单词列表并选择 2(对)的所有组合。
    • 这确实有效地构建了一个矩阵,但使用坐标 ('word1', 'word2') 作为键和计数作为值,将其展平为一个类似字典的对象
    • 这里还有一个问题是“manhattan”,“manhattan”的频率为16。这并不意味着这个PAIR在给定文本中存在16次(很可能它只是一个单词“ manhattan" 有 16 次。所有这样的对必须有一个真实的频率或只有 1。这怎么能改变?我也找不到一对 "manhattan", "oklahoma" 在本文中出现 8 次。
    • 可能itertools.combinations 是更好的匹配 - 替换
    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多