乘以列表python中的所有整数?

时间:2016-10-20 23:11:03

标签: python python-3.x

我想将python中列表列表中的所有值相乘:

    input = 3*[[1,2,3],[3,2,1]]

所以我得到了这个输出:

    output = [[3,6,9],[9,6,3]]

我试过这个:

    l = [[1,2,3],[3,2,1]]
    l = [i * 2 for i in x in l]

2 个答案:

答案 0 :(得分:3)

[[ 3 * i for i in inner ] for inner in outer]

答案 1 :(得分:1)

>>> my_list = [[1,2,3],[3,2,1]]
>>> [map((3).__mul__, sublist) for sublist in my_list]
[[3, 6, 9], [9, 6, 3]]

您可以考虑使用numpy数组:

>>> import numpy as np
>>> a = np.array(my_list)
>>> a
array([[1, 2, 3],
       [3, 2, 1]])
>>> 3*a
array([[3, 6, 9],
       [9, 6, 3]])