简体   繁体   English

在 python 中的特定索引之后,在嵌套列表中将元素相乘

[英]Multiplying elements together inside a nested list after a specific index in python

I have a nested list and I'd like to multiply all the elements, after a certain index, inside the list with each other.我有一个嵌套列表,我想在某个索引之后将列表内的所有元素相互相乘。 Like for example if my list is like this, (where all the elements inside (a1,f,g etc) are variables)例如,如果我的列表是这样的,(其中所有元素(a1,f,g 等)都是变量)

A=[[a1,a2,a3,f, g, g ],[b1, b2,b3, d, g, d]]

Id like for all the nested elements the third element to be multiplied together.对于所有嵌套元素,我希望将第三个元素相乘。 So the output would be as a such,所以 output 就是这样,

A=[[a1,a2,a3,f*g**2], [b1, b2, b3, d**2*g]]

In addition, Im wondering if theres a way to separately multiply the remaining elements with the product at the end of each nested list,另外,我想知道是否有一种方法可以将剩余元素分别与每个嵌套列表末尾的乘积相乘,

A=[[a1*f*g**2,a2*f*g**2, a3*f*g**2], [b1*d**2*g, b2*d**2*g, b3*d**2*g]]

Im not really sure how to begin approaching this so any help is appreciated.我不太确定如何开始处理这个问题,因此感谢您的帮助。

For the first part of the question, you can use a list comprehension to iterate over the sublists of A, followed by list indexing and the reduce function to compute what is required对于问题的第一部分,您可以使用列表推导来迭代 A 的子列表,然后使用列表索引和减少 function 来计算所需的内容

import functools

A = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
B = [sublist[:3] + [functools.reduce(lambda x, y: x*y, sublist[3:])] for sublist in A]

print(B)

For the second part its better to do this with an explicit for loop so that the product of the end elements each sublist is only computed once对于第二部分,最好使用显式的 for 循环来执行此操作,以便每个子列表的末端元素的乘积仅计算一次

C = []
for sublist in A:
    end_product = functools.reduce(lambda x, y: x*y, sublist[3:])
    C.append([item*end_product for item in sublist[:3]])
print(C)

In the end this gives最后这给了

A = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
B = [[1, 2, 3, 20], [6, 7, 8, 990]]
C = [[20, 40, 60], [5940, 6930, 7920]]

Stealing the other answer's data:窃取另一个答案的数据:

>>> from math import prod
>>> A = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]
>>> for a in A:
        a[3:] = prod(a[3:]),

>>> A
[[1, 2, 3, 20], [6, 7, 8, 990]]

And with our without the above step:而我们没有上述步骤:

>>> [[x * m for x in a[:3]] for a in A for m in [prod(a[3:])]]
[[20, 40, 60], [5940, 6930, 7920]]

Not sure if this is what you are looking for, but you can do it using a list comprehension and numpy:不确定这是否是您要查找的内容,但您可以使用列表理解和 numpy 来完成:

from numpy import product

index = 5
A = [[5, 5, 2, 3, 4, 2, 1, 1, 2], [2, 4, 2, 3, 2, 4, 5, 7], [2, 3, 5, 6, 3, 5, 3, 2]]

result = [[b for i, b in enumerate(B) if i < index] + [product(B[index:])] for B in A]

print(result) # [[5, 5, 2, 3, 4, 4], [2, 4, 2, 3, 2, 140], [2, 3, 5, 6, 3, 30]]

and for your second question:对于你的第二个问题:

result2 = [[r * R[-1] for r in R[:-1]] for R in result]
print(result2) # [[20, 20, 8, 12, 16], [280, 560, 280, 420, 280], [60, 90, 150, 180, 90]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM