微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

多个 TypeErrors 试图将列表推导式中的列表元素相乘

如何解决多个 TypeErrors 试图将列表推导式中的列表元素相乘

我正在关注一本关于深度学习的书,这本书鼓励理解更方便的 numpy 替代方案背后的基本数学运算,以便更好地理解基本原理。我试图在本机 Python 3.9 中重建 numpy 的乘法 (*) 运算符,但我遇到了一些我觉得非常困惑的类型错误。希望有人能帮忙。

def multiply(a,b):

    """ 1.) float * float
        2.) float * vector
        3.) float * matrix
        4.) vector * vector
        5.) vector * matrix
        6.) matrix * matrix """

    def float_float(float_a,float_b):
        return float_a * float_b
    def float_vector(float_a,vector_b):
        return [float_float(float_a,component_b) for component_b in vector_b]
    def float_matrix(float_a,matrix_b):
        return [float_vector(float_a,vector_b) for vector_b in b]
    def vector_vector(vector_a,vector_b):
        return [a * b for a,b in dict(zip(vec_a,vec_b)).items()]
    def vector_matrix(vector_a,matrix_b):
        return [vector_vector(vector_a,vector_b) for vector_b in matrix_b]
    def matrix_matrix(matrix_a,matrix_b):
        return [vector_vector(a,b) for a,b in dict(zip(matrix_a,matrix_b)).items()]

    def get_type(operand):
        if type(operand) == float:
            return "float"
        elif type(operand) == list:
            if any(isinstance(item,list) for item in operand):
                return "matrix"
            else:
                return "vector"

    types = (get_type(a),get_type(b))
    print(types)

    operations_table = {
        ("float","float"): float_float(a,b),("float","vector"): float_vector(a,("vector","float"): float_vector(b,a),"matrix"): float_matrix(a,("matrix","float"): float_matrix(b,"vector"): vector_vector(a,"matrix"): vector_matrix(a,"vector"): vector_matrix(b,"matrix"): matrix_matrix(a,b)
    }

    return operations_table[types]

# float
f = 2.0
# vector
v = [0.5,1.0,2.0]
# matrix
m = [
    [1.0,2.0,3.0],[4.0,5.0,6.0],[7.0,8.0,9.0]
]

# TEST
print(multiply(f,f))
print(multiply(f,v))
print(multiply(v,m))

这是我在尝试多个 2 个浮点数时遇到的第一个 TypeError:

('float','float')
Traceback (most recent call last):
  File "e:\OneDrive\Projects\Deep_Learning\CH05\CH05-000_NativeOps.py",line 62,in <module>
    print(multiply(f,f))
  File "e:\OneDrive\Projects\Deep_Learning\CH05\CH05-000_NativeOps.py",line 38,in multiply
    ("float",File "e:\OneDrive\Projects\Deep_Learning\CH05\CH05-000_NativeOps.py",line 14,in float_vector
    return [float_float(float_a,component_b) for component_b in vector_b]
TypeError: 'float' object is not iterable

这是我尝试乘以 float * vector 时遇到的第二个 TypeError

('float','vector')
Traceback (most recent call last):
  File "e:\OneDrive\Projects\Deep_Learning\CH05\CH05-000_NativeOps.py",line 63,v))
  File "e:\OneDrive\Projects\Deep_Learning\CH05\CH05-000_NativeOps.py",line 37,line 12,in float_float
    return float_a * float_b
TypeError: can't multiply sequence by non-int of type 'float'

非常感谢任何帮助,因为在示例中,变量 f、v 和 m 中的所有值都是专门为浮点数设置的,因此我对遇到的错误类型感到非常惊讶。谢谢!!!

解决方法

问题出在那个operations_table 字典上。它导致所有版本的乘法始终运行,无论操作数类型如何。我使用 eval() 将其更改为更短的解决方案,现在代码完美运行。

def multiply(a,b):

    """ 1.) float * float
        2.) float * vector
        3.) float * matrix
        4.) vector * vector
        5.) vector * matrix
        6.) matrix * matrix """

    def float_float(float_a,float_b):
        return float_a * float_b
    def float_vector(float_a,vector_b):
        return [float_float(float_a,component_b) for component_b in vector_b]
    def float_matrix(float_a,matrix_b):
        return [float_vector(float_a,vector_b) for vector_b in b]
    def vector_vector(vector_a,vector_b):
        return [a * b for a,b in list(zip(vector_a,vector_b))]
    def vector_matrix(vector_a,matrix_b):
        return [vector_vector(vector_a,vector_b) for vector_b in matrix_b]
    def matrix_matrix(matrix_a,matrix_b):
        return [vector_vector(a,b) for a,b in list(zip(matrix_a,matrix_b))]

    def get_type(operand):
        if type(operand) == float:
            return "float"
        elif type(operand) == list:
            if any(isinstance(item,list) for item in operand):
                return "matrix"
            else:
                return "vector"

    types = (get_type(a),get_type(b))
    print(types)
    result = eval(f"{types[0]}_{types[1]}(a,b)")
    return result

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?