How to apply parameters/for loop

前端 未结 2 1422
误落风尘
误落风尘 2021-01-21 10:42

Currently I have a program that finds the dot product of two XYZ coordinates, how would I put this into a loop, so that it goes down a list of coordinates and finds the dot prod

相关标签:
2条回答
  • 2021-01-21 11:14
    import numpy
    v0 = (6,7,6)
    vectors = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
    dot_products = vectors.dot(v0)
    

    is by far the easiest way to do what you are trying to do imho

    and it should be much faster if you have "a lot" of vectors in your vectors list...(for some definition of a lot)

    0 讨论(0)
  • 2021-01-21 11:19
    import operator
    
    vector1 = (1, 2, 3)
    
    # get a list of vectors
    vectors = [
        (4, 5, 6),
        (7, 8, 9)
    ]
    
    # for loop through the vectors,
    # assignig the current vector to vector2 in every iteration
    for vector2 in vectors:
        dotProduct = reduce(operator.add, map(operator.mul, vector1, vector2))
        print dotProduct
    

    Using your l, nat and a variables:

    vector1 = (int(l[0][0]), int(l[0][1]), int(l[0][2]))
    
    for a in range(1, nat):
        vector2 = (int(l[a][0]), int(l[a][1]), int(l[a][2]))
        dotProduct = reduce(operator.add, map(operator.mul, vector1, vector2))
        print(dotProduct)
    
    0 讨论(0)
提交回复
热议问题