求除自身以外数组的乘积

April 1, 2019 · 吐槽 · 48次阅读
input = [1,2,3,4]
# output = [24,12,8,6]
input = [5,3,2,4,1]
# output = [24,40,60,30,120]
length = len(input)
a = b = []
for index in range(length):
    r=1
    for i in input[:index]:
        r *= i
    a.append(r)
    r=1
    for i in input[length-index-1:]:
        r *= i
    b.append(r)
b.reverse()
b = b + [1]
_ = b.pop(0)
# print(a)
# print(b)
print(list(map(lambda x, y: x * y, a, b)))

这种算是知道就会,不知道也很难想出来的吧?

标签:none

最后编辑于:2019/04/01 15:21

添加新评论