Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

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

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

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...