max_dot_p如果串列中的向量超過 3 個,我該如何修改我的代碼以便作業?(dot_p是點積)
這是我嘗試過的:
def dot_p(vector1, vector2):
total = 0
for x, y in zip(vector1, vector2):
total = x * y
return total
def max_dot_p(vectors):
product = []
for i in range(len(vectors)):
for j in range(len(vectors)):
dot_p = dot_product(vectors[i] , vectors[j])
product.append(dot_p)
continue
max_product = max(product)
return max_product
if __name__ == "__main__":
vectors = [[5, 6], [13, 1], [3, 1]
print(max_dot_p(vectors))
它沒有給我預期的答案,雖然它運行
uj5u.com熱心網友回復:
您可以使用itertools.combinations選擇兩個向量來傳遞給您的點積函式,然后將所有呼叫中的最大值傳遞給您的點積函式:
from itertools import combinations
def max_dot_p(vectors):
return max(dot_p(x, y) for x, y in combinations(vectors, k=2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/439687.html
上一篇:關于C 單鏈表實作的問題
