列表中的数字乘法(Python)

列表中的数字乘法(Python),python,list,multiplication,Python,List,Multiplication,Python中是否有函数可以将列表中的数字相乘 input -> A = [1,2,3,4] output -> B = [1*1, 1*2, 1*3, 1*4, 2*2, 2*3, 2*4, 3*3, 3*4, 4*4] 或者有人能帮我创建自己的函数吗?我有8000多条记录,我不想手动操作 到目前为止,我唯一想到的是: for i in list: list[i] * list[i+1] 但我知道这是行不通的,我不知道如何处理这些数据。这是一种方法 A = [1,2,

Python中是否有函数可以将列表中的数字相乘

input -> A = [1,2,3,4]

output -> B = [1*1, 1*2, 1*3, 1*4, 2*2, 2*3, 2*4, 3*3, 3*4, 4*4]
或者有人能帮我创建自己的函数吗?我有8000多条记录,我不想手动操作

到目前为止,我唯一想到的是:

for i in list:
    list[i] * list[i+1]
但我知道这是行不通的,我不知道如何处理这些数据。

这是一种方法

A = [1,2,3,4]

res = [i*j for i in A for j in A[A.index(i):]]

# [1, 2, 3, 4, 4, 6, 8, 9, 12, 16]
替代解决方案:

n = len(A)
res = [A[i]*A[j] for i in range(n) for j in range(i, n)]

以下是使用from的另一种方法:


如果下列解决方案中的一个解决了你的问题,请考虑接受(左边的绿色蜱),或者自由地要求澄清。
>>> A = [1,2,3,4]
>>> from itertools import combinations_with_replacement
>>> [a * b for a, b in combinations_with_replacement(A, 2)]
[1, 2, 3, 4, 4, 6, 8, 9, 12, 16]