【问题标题】:TypeError not supported between instances of 'type' and 'int''type' 和 'int' 的实例之间不支持 TypeError
【发布时间】:2020-12-30 20:34:05
【问题描述】:

我正在尝试将矩阵设为 2 维,然后将矩阵的 1 个元素进行比较。作为记录,我用整数类型声明我的矩阵,并且我也与整数进行比较。这是怎么回事?这是我的矩阵声明:

Matkul = [str]*10
for i in range (10) :
    Matkul[i] = [int]*2

那么这是我在 matkul 中的整数元素与整数之间的比较:

x = Matkul[i][1]
if   x >= 85 :
        return 'A'

谢谢

【问题讨论】:

    标签: python-3.x matrix


    【解决方案1】:

    当您使用 str 时,您不会创建字符串,您只需复制 what 字符串,int 也是如此。 如果你想创建一个整数,使用int()(和str()作为字符串),或者简单地说:

    Matkul = []
    for _ in range (10) : # It's better practice to use _ (underscore) instead of i in this case to specify that you won't actually use it
        Matkul.append([0]*2)
    

    甚至,如果您了解列表理解:

    Matkul = [[0]*2 for _ in range(10)]
    

    【讨论】:

      猜你喜欢
      • 2020-09-07
      • 2018-09-03
      • 2020-11-13
      • 2021-01-15
      • 2020-06-02
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多