Problem in creating a vector - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Problem in creating a vector (/thread-21476.html) Pages:
1
2
|
Problem in creating a vector - termo - Oct-01-2019 I need to create a vector A_vec. The code line is: self.A_vec = [self.B[0:j], self.B[j] + self.C / self.D, self.B[j+1:len(self.E)]]and the error is: "unsupported operand type(s) for -: 'float' and 'list'" I think the problem are the expression as self.B[0:j]but I don't know how to solve it. The meaning of that expression is to take values from self.B[0]to self.B[j]where j is a int number es 2. I tried also with self.B[range(0,j)]but it doesn't work. RE: Problem in creating a vector - stullis - Oct-01-2019 We're going to need more information. Please post the full error traceback and a little bit more of your code. Based on the error, I suspect the problem is actually "self.B[j] + self.C / self.D". The error concerns and "unsupported operand" meaning there's a mathematical operation that is not recognized between two data types. Based on the rest of this list, the capital letter fields are sequences and adding the value of self.B to self.C would make sense. I cannot confirm that without more information from your code, however. RE: Problem in creating a vector - termo - Oct-02-2019 This is the code (sorry for the crazy notation): self.H_ev_hf_vec = [self.H_ev_ex_l_hf, self.H_ev_su_v_hf] self.H_ev_wf_vec = [self.H_ev_su_l_wf, self.H_ev_ex_l_wf, self.H_ev_su_v_wf, self.H_ev_ex_v_wf] j = 0 while j < max(len(self.H_ev_hf_vec), len(self.H_ev_wf_vec)) - 2: self.Q_ev_hf = self.m_hf * (self.H_ev_hf_vec[j+1] - self.H_ev_hf_vec[j]) self.Q_ev_wf = self.m_wf * (self.H_ev_wf_vec[j+1] - self.H_ev_wf_vec[j]) if self.Q_ev_hf > self.Q_ev_wf: self.H_ev_hf_vec = [self.H_ev_hf_vec[0:j], self.H_ev_hf_vec[j] + self.Q_ev_wf / self.m_hf, self.H_ev_hf_vec[j+1:len(self.H_ev_wf_vec)]] else: self.H_ev_wf_vec = [self.H_ev_wf_vec[0:j], self.H_ev_wf_vec[j] + self.Q_ev_hf / self.m_wf, self.H_ev_wf_vec[j+1:len(self.H_ev_hf_vec)]] j = j+1self.m_hf and self.m_wf are numbers And the error is: Cycle code output error: TypeError("unsupported operand type(s) for -: 'float' and 'list'") RE: Problem in creating a vector - scidam - Oct-02-2019 Consider using NumPy. NumPy is a great (and fast) tool for vector/matrix manipulations. RE: Problem in creating a vector - termo - Oct-02-2019 Thank you for the suggestion, I'll study numpy more. But now the problem is the line self.H_ev_hf_vec = [self.H_ev_hf_vec[0:j], self.H_ev_hf_vec[j] + self.Q_ev_wf / self.m_hf, self.H_ev_hf_vec[j+1:len(self.H_ev_wf_vec)]]Could the expression self.H_ev_hf_vec[j+1:len(self.H_ev_wf_vec)]be wrong from a writing point of view? RE: Problem in creating a vector - stullis - Oct-02-2019 This: self.H_ev_hf_vec[j+1:len(self.H_ev_wf_vec)]cannot be the problem. "j" is an integer and so is 1; len() returns an integer as well. That does not match the error which states a problem between a float (not an integer) and a list. That's why I suspect the error is: self.H_ev_hf_vec[j] + self.Q_ev_wf / self.m_hfWhat are the data types of each of those fields? RE: Problem in creating a vector - termo - Oct-02-2019 self.Q_ev_wfand self.m_hfare both float self.H_ev_hf_vec[j]is a numpy.float64 But the probem remains also if I delete + self.Q_ev_wf / self.m_hf RE: Problem in creating a vector - stullis - Oct-02-2019 Okay, after reviewing this for a bit, I believe the data types are changing during the loop, which is the major problem with dynamic data types. The range slices (e.g. list[0:j]) return lists of items or even an empty list. So, I suspect the following:
I ran a short test to confirm on my end using the same logic basic logic as your loop: x = [1,2,3] for j in range(2): print(x) x = [x[0:j], x[j] + 5.0 / 2.5, x[j+1:]] # Output [1, 2, 3] [[], 3.0, [2, 3]]To verify, add a print() call to print the contents of self.H_ev_hf_vec and self.H_ev_wf_vec. The loop you have above is infinite because "j=j+1" is on a different indentation level. Also, avoid while loops any time you can; I refactored the while loop into a for loop for you. self.H_ev_hf_vec = [self.H_ev_ex_l_hf, self.H_ev_su_v_hf] self.H_ev_wf_vec = [self.H_ev_su_l_wf, self.H_ev_ex_l_wf, self.H_ev_su_v_wf, self.H_ev_ex_v_wf] for j in range(max(len(self.H_ev_hf_vec), len(self.H_ev_wf_vec)) - 2): self.Q_ev_hf = self.m_hf * (self.H_ev_hf_vec[j+1] - self.H_ev_hf_vec[j]) self.Q_ev_wf = self.m_wf * (self.H_ev_wf_vec[j+1] - self.H_ev_wf_vec[j]) if self.Q_ev_hf > self.Q_ev_wf: self.H_ev_hf_vec = [self.H_ev_hf_vec[0:j], self.H_ev_hf_vec[j] + self.Q_ev_wf / self.m_hf, self.H_ev_hf_vec[j+1:len(self.H_ev_wf_vec)]] else: self.H_ev_wf_vec = [self.H_ev_wf_vec[0:j], self.H_ev_wf_vec[j] + self.Q_ev_hf / self.m_wf, self.H_ev_wf_vec[j+1:len(self.H_ev_hf_vec)]] RE: Problem in creating a vector - termo - Oct-03-2019 Why in your example x = [1,2,3] for j in range(2): print(x) x = [x[0:j], x[j] + 5.0 / 2.5, x[j+1:]]the second output is not "[[1], 3.0, [2, 3]]"? The first number it should write is 1. Is there a way to concatenate list to float? I can't find anyone. And it seems me the only way to achieve my gole, that is the vector: self.H_ev_hf_vec = [self.H_ev_ex_l_hf, self.H_ev_ex_l_hf + A / self.m_hf, (self.H_ev_ex_l_hf + A / self.m_hf) + B / self.m_hf, self.H_ev_su_v_hf]where A = self.m_wf * (self.H_ev_wf_vec[1] - self.H_ev_wf_vec[0]) and B = self.m_wf * (self.H_ev_wf_vec[2] - self.H_ev_wf_vec[1]) RE: Problem in creating a vector - stullis - Oct-03-2019 The first index is [] instead of 1 because list[0:0] extracts all the values between index 0 and ending at index 0. Because the beginning and end are the same index, there are no values between them. List[0:1] would extract [1]. To concatenate a value to a list, you can use the list.append() method. |