Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
289 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...