如何将列表中的第一个元素乘以Python中的数字(在列表列表中)?

时间:2016-03-09 03:03:39

标签: python

我有一个像这样的列表列表:

[[1,2,3],[4,5,6]]

我想将每个内部列表的第一个元素乘以10.所以预期的输出将是:

[[10,2,3],[40,5,6]]

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

试试这个:

//...
{
    float arrayName[length][width]; 
    // use arrayName here

    //... still in-scope
} // scope limit
// all of arrayName released from stack 

{
    // stack is available for other use, so try
    uint32_t  u32[3][length][width]; 
    // use u32 here


    //... still in-scope
} // scope ended
// all of u32 released from stack 

// better yet, use std::vector or another container
std::vector<uint32_t>  bigArry;

输出:

my_list = [[1,2,3],[4,5,6]]

for i in my_list:
    i[0] *= 10  # multiply the first element of each of the inner lists by 10

print(my_list)

答案 1 :(得分:1)

如果你正在使用大型数组,那么numpy也比单独使用python更好,原因还有很多其他原因。

lst = [[1,2,3],[4,5,6]] # if you have your list in python

lst = np.array(lst) # simple one-liner to convert to an array

lst
Out[32]: 
array([[1, 2, 3],
       [4, 5, 6]])

lst[:,0] *= 10 # multiply first column only by 10. This removes the need
               # for python's "for" loops, which improves performance 
               # on larger arrays

lst
Out[34]: 
array([[10,  2,  3],
       [40,  5,  6]])