首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python以不同形式打印矢量

python以不同形式打印矢量
EN

Stack Overflow用户
提问于 2021-05-29 09:26:33
回答 1查看 46关注 0票数 0

编写一个名为‘vectormath.py’的程序,在3个维度上进行基本的向量计算:加法、点积和归一化。

我的映射函数有一个错误。

示例输入:

代码语言:javascript
复制
Enter vector A:

1 3 2

Enter vector B:

2 3 0

样本输出

代码语言:javascript
复制
A+B = [3, 6, 2]

A.B = 11

|A| = 3.74

|B| = 3.61

代码:

代码语言:javascript
复制
import math
def addition(a,b):
    return[a[0]+b[0] , a[1] + b[1] , a[2] + b[2]]

def  Dotproduct(a , b):
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]

def norm(a):
    square = a[0]**2 + a[1]**2 + a[2]**2
    return math.sqrt(square)

def main():
    vector_A = input("Enter vector A:\n").split(" ")
    vector_A = map(int,vector_A)
    vector_B = input("Enter vector B:\n").split(" ")
    vector_B = map(int, vector_B)
    
    print(addition(vector_A,vector_B))
    print(addition(vector_A,vector_B))
    print(norm(vector_A))
    print(norm(vector_B))
    
if __name__ == "__main__":
    main()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-29 12:51:57

替换

使用vector_A = list(map(int, vector_A))vector_A = map(int,vector_A)

使用vector_B = list(map(int, vector_B))vector_B = map(int,vector_B)

在这里,map(int, vector_A)返回一个地图对象,它是一个迭代器。

list()获取迭代器并返回代码中需要的列表,以便进行进一步的向量计算。

到目前为止,您正在向函数addition, Dotproduct and norm传递map object,它实际上应该是定义的计算的列表。

上面的更改将贴图对象转换为列表。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67746974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档