numpy 将list乘以列表中除其自身索引之外的所有元素

des4xlb0  于 2022-11-29  发布在  其他
关注(0)|答案(9)|浏览(121)

我试图建立一个函数,给定一个列表,它将返回一个元素相乘的列表,不包括索引相同的元素。例如,对于列表[1,2,3,4],它将返回[2*3*4,1*3*4,1*2*3]。这就是我所尝试的

import numpy as np
def my_function(ints):
products = np.ones(len(ints))
indices = range(0,len(ints))
for i in ints:
    if i != indices[i]
        products *=ints[i]
return products

我想我的问题是我在想"我"会引用指数而不是这些指数的值。
1.如何创建引用索引而不是值的对象
1.有没有更好的方法来解决这个问题?

nbnkbykc

nbnkbykc1#

对于numpy,这很简单:

import numpy as np
def fun(input):
    arr = np.array(input)
    return arr.prod() / arr
bsxbgnwa

bsxbgnwa2#

摘自@interjay的评论:

from operator import mul

total = reduce(mul, ints)
multiplied = [total/y for y in ints]
oug3syen

oug3syen3#

enumerate的解决方案:

def my_function(ints):
    res = []
    for i, el in enumerate(ints):
        res.append(reduce(lambda x, y: x*y, ints[:i] + ints[i+1:]))
    return res

print my_function([1,2,3,4])
>>> [24, 12, 8, 6]
jmo0nnb3

jmo0nnb34#

使用列表解析

array_of_nums = np.random.rand(10)
[np.prod([i for x, i in enumerate(array_of_nums) if x != e]) for e, y in enumerate(array_of_nums)]
cyej8jka

cyej8jka5#

不使用除法

import numpy as np
    
    def remainingProduct(arr):
        a = arr
        list1 = []
        n = len(arr)
        for i in range(0, n):
            b = a[0]
            a.remove(a[0])
            c = np.prod(a)
            list1.append(c)
            a.append(b)
        return list1
    
    # print(remainingProduct([3, 2, 1]))#[2, 3, 6].
ep6jt1vc

ep6jt1vc6#

"这里有一个简单的方法"

def mul_numbers(nums):
    new_arr = []
    for i in range(len(nums)):
        product = 1
        for j in range(len(nums)):
            if nums[i] != nums[j]:
                product *= nums[j]
        new_arr.append(product)

    return new_arr

arr = [1, 2, 3, 4, 5]
result = mul_numbers(arr)
print(result)

>>> [120, 60, 40, 30, 24]
kqqjbcuj

kqqjbcuj7#

下面是一个使用reduce的方法:

from functools import reduce
from operator import mul

l = [1, 2, 2, 3, 4]
# Modified to work with lists that contain dupes thanks to the feedback in comments
print [reduce(mul, l[:i]+l[i+1:], 1) for i in xrange(len(l))]
>>> [48, 24, 24, 16, 12]
i7uaboj4

i7uaboj48#

将枚举合并到代码中的方法是

import numpy as np
def intmult(ints):
    products = np.ones(len(ints))
    for i,j in enumerate(ints):
        products *= ints[i]
    for i,j in enumerate(products):
        products[i] /= ints[i]
    return products

请注意,这不包括其中一个元素为0的情况。

nxagd54h

nxagd54h9#

不使用任何库

只是不要忘记先用适当缩进为循环追加新数组。

def f(arr):
    new_arr = []
    for i in arr: #loop first
        p = 1
        for j in arr:
            if i!=j:
                p =  p*j
        new_arr.append(p) #append for loop first
    return new_arr
arr = [2, 3, 4,5]
f(arr)

有点混乱,但很有效[不推荐]

def f(arr):
    n = len(arr)
    left = [1]*n
    right = [1]*n
    product_array = []

    # build left array
    for i in range(1,n):
        left[i] = left[i-1] * arr[i-1]
    # build right array
    for i in range(1,n):
        right[i] = right[i-1] * arr[::-1] [i-1]
    
    # build product arr from subarrays
    for i in range(n):
        product_array.append(left[i] * right[::-1][i])
    return product_array

arr = [2, 3, 4,5]
f(arr)

相关问题