gpt4 book ai didi

python - python 中 itertools.product 的最小值

转载 作者:行者123 更新时间:2023-11-28 22:44:22 24 4
gpt4 key购买 nike

import itertools

A = [50, 60, 70]
B = [0.3, 0.1, 0.5, 0.4]

print [a + b for a, b in itertools.product(A, B)]

>> [50.3,50.1,50.5,50.4,60.3,60.1,60.5,60.4,70.3,70.1,70.5,70.4]

在上面的代码中,有没有办法只为每个 a 返回 a + b 的最小值?所以输出应该是:

[50.1,60.1,70.1]

请注意这是一个玩具示例,在真实示例中,A 和 B 是经纬度值列表,我计算它们之间的大圆距离

最佳答案

是的,首先选择B的最小值,然后将其添加到A的每个元素。

b = min(B)
print([a + b for a in A])

关于python - python 中 itertools.product 的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29578795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com