index 22 is out of bounds for axis 1 with size 22

def frequency(wordlist, totallist):
    arr = np.zeros(shape=(2681, 22933), dtype=int)

    i = 0
    for line in wordlist:

        n = list(Counter(line).keys())
        v = list(Counter(line).values())
        j = 0
        m = 0
        for elem in totallist:
            if elem in n:
                arr[i, j] = v[m]  
                m += 1
           
            j += 1

        i += 1

    print(arr)

原代码报错index 22933 is out of bounds for axis 1 with size 22933

def test_frequency():
    wordlist = [['apple'], ['apple', 'apple'], ['tree', 'water'], ['argue', 'apple','apple','tree','tree','tree']]
    totallist = ['apple', 'tree', 'argue', 'water']
    arr = np.zeros(shape=(4, 4), dtype=int) 

    i = 0
    for line in wordlist:

        n = list(Counter(line).keys())
        v = list(Counter(line).values())
        j = 0
        m = 0
        for elem in totallist:
            if elem in n:
                arr[i, j] = v[m]  
                m += 1
            j += 1

        i += 1

    print(arr)

缩减矩阵规模,可以运行。

循环语句数组越界了,

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y