multiply list of ndarrays by list

2024/11/17 5:49:56

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]
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)
https://en.xdnf.cn/q/120240.html

Related Q&A

Python skipping last element in while iterating a list using for loop [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 9 years ago.class Sample:def __init__(self):self.lst_report_foot…

can this code be shortened or improved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Count Running and Stopped Ec2 Instances with AWS Lambda

How can I count number of running and stopped EC2 instances in a particular region using boto3 and an AWS Lambda function?

Python2.7: How to split a column into multiple column based on special strings like this?

Im a newbie for programming and python, so I would appreciate your advice!I have a dataframe like this.In info column, there are 7 different categories: activities, locations, groups, skills, sights, t…

python - return and print does not give same result

what Im trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.def main():myDict = {a:1, b:2, c:3, d:4, e:5....}u_input…

Creating a menuBar in a frame?

import Tkinter as tk from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas import subprocess from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Yclass SampleApp(tk.Tk):def __ini…

Python return dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Use selenium and python to move mouse outside of web page area

I need to test the exit intent form on this page: https://www.graphicproducts.com/guides/5s-system/When the mouse pointer is moved outside of the web page area a popup window appears. I then need to en…

Python „stack overflow” (104 if statements). Is def(x) the only solution to optimise code?

Today have tried to check files with path directory name. Previously it worked, until I tried to create 104 if/else statements. How to dispose of this overflow error? More specific question: Does def(…

How to fix Python restarting whenever I start program on IDLE environment?

import randomwinning_conditon = 0 no_of_guesses = 0 comp_guess = random.randint(1,100)while (no_of_guesses == 11) or (winning_conditon == 1):user_guess = int(input("What is your guess? "))if…