You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

普通numpy数组为何引发TypeError与SyntaxWarning?

问题分析:列表索引类型错误与NumPy数组构造问题

问题描述

请问有人能解释我为何会收到如下警告:

<>:18: SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?

以及如下错误信息:

TypeError                                 Traceback (most recent call last)
Cell In[62], line 18
     12         weights.extend([w / d] * d)
     14     return maut_method(D, weights, ['min'] * 54 + ['max'] * 10 + ['min'] * 1 + ['max'] * 1 + ['min'] * 1, ['exp'] * 67, graph=True) #poistin tästä 0.01 joka on varmaan step funktion koko
     17 dataset = np.array([
---> 18                     [1, 1, 1, 1, 1, 2, 2, 2, 3, 59.4, 4.13, 4, 4, 2, 3, 4, 4, 1, 1, 1, 1, 1, 1, 1550, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.43, 187, 1.87e-05, 0.0698, 0.149, 1, 0.0398, 1, 1, 1, 1, 1, 1, 315, 6030, 1, 2910, 0.00134, 1, 183, 27.2, 30.6, 3, 48, 3, 23, 14, 3.3, 1, 3.65, 0.025, 2, 0.0] #SnPb
     19 [1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 4.13e-05, 0.551, 1.47, 1, 0.12, 1, 1, 1, 1, 1, 1, 768, 8760, 1, 5770, 0.0214, 1, 218, 28, 32, 3.4, 22.1, 3, 27, 17, 13, 5, 8.936, 0.304, 2, 0.0] #SAC water
     20 [1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 4.13e-05, 0.551, 1.47, 1, 0.12, 1, 1, 1, 1, 1, 1, 768, 8760, 1, 5770, 0.0214, 1, 218, 20, 30, 6.2, 26.1, 3, 27, 17, 13, 5, 8.936, 0.304, 2, 0.0] #SAC air
     21 [1, 1, 1, 1, 1, 2, 2, 2, 3, 54.9, 2.58, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 8.7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.53, 216, 1.78e-05, 0.0706, 0.199, 1, 0.0364, 1, 1, 1, 1, 1, 1, 312, 5830, 1, 3400, 0.00133, 1, 227, 15, 19, 5.4, 20.8, 2, 21.5, 18.5, 8.6, 2.1, 1.125, 0.41, 2, 0.0] #SnCu water
     22 [1, 1, 1, 1, 1, 2, 2, 2, 3, 54.9, 2.58, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 8.7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.53, 216, 1.78e-05, 0.0706, 0.199, 1, 0.0364, 1, 1, 1, 1, 1, 1, 312, 5830, 1, 3400, 0.00133, 1, 227, 16, 22, 9.1, 41.2, 2, 21.5, 18.5, 8.6, 2.1, 1.125, 0.41, 2, 0.0] #SnCu air
     23                    ])
     27 print(get_ranks(dataset))

TypeError: list indices must be integers or slices, not tuple

我认为自己的数据是包含整数与小数的合法numpy数组,请问问题出在哪里?以下是我的完整代码:

import pyDecision
import numpy as np
from pyDecision.algorithm import maut_method


def get_ranks(D):
    Ws, Ds = [13.75, 20.83, 18.75, 18.33, 14.58, 13.75], [5, 18, 10, 20, 11, 3]
    weights = []
    for w, d in zip(Ws, Ds):
        weights.extend([w / d] * d)

    return maut_method(D, weights, ['min'] * 54 + ['max'] * 10 + ['min'] * 1 + ['max'] * 1 + ['min'] * 1, ['exp'] * 67, graph=True) 


dataset = np.array([
                    [1, 1, 1, 1, 1, 2, 2, 2, 3, 59.4, 4.13, 4, 4, 2, 3, 4, 4, 1, 1, 1, 1, 1, 1, 1550, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.43, 187, 1.87e-05, 0.0698, 0.149, 1, 0.0398, 1, 1, 1, 1, 1, 1, 315, 6030, 1, 2910, 0.00134, 1, 183, 27.2, 30.6, 3, 48, 3, 23, 14, 3.3, 1, 3.65, 0.025, 2, 0.0] #a1
[1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 4.13e-05, 0.551, 1.47, 1, 0.12, 1, 1, 1, 1, 1, 1, 768, 8760, 1, 5770, 0.0214, 1, 218, 28, 32, 3.4, 22.1, 3, 27, 17, 13, 5, 8.936, 0.304, 2, 0.0] #a2
[1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 4.13e-05, 0.551, 1.47, 1, 0.12, 1, 1, 1, 1, 1, 1, 768, 8760, 1, 5770, 0.0214, 1, 218, 20, 30, 6.2, 26.1, 3, 27, 17, 13, 5, 8.936, 0.304, 2, 0.0] #a3
[1, 1, 1, 1, 1, 2, 2, 2, 3, 54.9, 2.58, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 8.7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.53, 216, 1.78e-05, 0.0706, 0.199, 1, 0.0364, 1, 1, 1, 1, 1, 1, 312, 5830, 1, 3400, 0.00133, 1, 227, 15, 19, 5.4, 20.8, 2, 21.5, 18.5, 8.6, 2.1, 1.125, 0.41, 2, 0.0] #a4
[1, 1, 1, 1, 1, 2, 2, 2, 3, 54.9, 2.58, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 8.7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.53, 216, 1.78e-05, 0.0706, 0.199, 1, 0.0364, 1, 1, 1, 1, 1, 1, 312, 5830, 1, 3400, 0.00133, 1, 227, 16, 22, 9.1, 41.2, 2, 21.5, 18.5, 8.6, 2.1, 1.125, 0.41, 2, 0.0] #a5
                   ])


print(get_ranks(dataset))

问题原因与解决方案

  • 语法错误根源:构造NumPy数组时,内部的每个子列表(代表一行数据)之间缺少逗号分隔。Python解释器会把相邻的两个列表解析为列表[列表]的形式,也就是用后一个列表作为前一个列表的索引,而列表索引只能是整数或切片,不能是列表(元组类型的索引),这就触发了TypeError: list indices must be integers or slices, not tuple错误,同时提前抛出语法警告。

  • 修复方案:在每个子列表的末尾添加逗号,确保Python将它们识别为数组中的独立元素。示例修复后的代码片段:

dataset = np.array([
                    [1, 1, 1, 1, 1, 2, 2, 2, 3, 59.4, 4.13, 4, 4, 2, 3, 4, 4, 1, 1, 1, 1, 1, 1, 1550, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.43, 187, 1.87e-05, 0.0698, 0.149, 1, 0.0398, 1, 1, 1, 1, 1, 1, 315, 6030, 1, 2910, 0.00134, 1, 183, 27.2, 30.6, 3, 48, 3, 23, 14, 3.3, 1, 3.65, 0.025, 2, 0.0], #a1
                    [1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 4.13e-05, 0.551, 1.47, 1, 0.12, 1, 1, 1, 1, 1, 1, 768, 8760, 1, 5770, 0.0214, 1, 218, 28, 32, 3.4, 22.1, 3, 27, 17, 13, 5, 8.936, 0.304, 2, 0.0], #a2
                    [1, 1, 1, 1, 1, 2, 2, 2, 3, 57.5, 5.04, 3, 3, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1, 198, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 357, 
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 02:37:50