简体   繁体   中英

multiply list of ndarrays by list

I want to multiply a list of numbers, by a ndarray, that is, each number in the list multiply by the first ndarray. This is the list:

list1 = [840,845,897]

This is the list of ndarray

list = df['Example']
list
0     [[nan, nan, nan, 0.854,nan,0.562,0.21615],[nan, nan, nan, 0.854,nan,0.562,0.21615]]
1     [[nan, nan, nan, nan, nan, nan, 0.5196,0.56518,0.165],[nan, nan, nan, nan, nan, nan, 0.816,0.5565518,0.225]
2     [[nan, nan, nan, nan, nan, nan, nan, 0.8528845],[nan, 0.595, nan, nan, 0.2651, nan, nan, 0.8528845],[nan, nan, nan, nan, nan, nan, nan, 0.8516]


I want to multiply the first item in the list by the first ndarray. The nan values remain nan and just multiply the float numbers. I have tried with map, lambda function, iterations with for but I have not succeeded. The error that I get frequently is:TypeError: can't multiply sequence by non-int of type 'str'. It can create a separate column with the new data, or simply create a list. The result:

list_new
0     [[nan, nan, nan, 717.36,nan,472.08,181.566],[nan, nan, nan, 717.36,nan,472.08,181.5666]]
1     [[nan, nan, nan, nan, nan, nan, 439.062,477.5771,139.425],[nan, nan, nan, nan, nan, nan, 689.52,470.28,190.125]
2     [[nan, nan, nan, nan, nan, nan, nan, 765.037],[nan, 533.715, nan, nan, 237.79, nan, nan, 765.037],[nan, nan, nan, nan, nan, nan, nan, 763.8852]

You can loop over the array like so:

nd = np.array( ... )
for i, x in enumerate(list1):
    for y in nd[i]:
        for j in range(len(y)):
            y[j] *= x
print(nd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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