I have the following code in Python: The type hints are used upon instantiation of MyDesign to dynamically add instances of the various Modules. I chose this syntax because the class MyDesign is really just a “template” defining what Modules it’s composed of, but the instantiation of modules needs some arguments only available when MyDesign is being instantiated. I would
Tag: loops
How to create files from a groupby object, based on the length of the dataframe
I have a dataframe (df) that looks like this (highly simplified): The ‘VALUE’ column contains a variable number of rows with identical values. I am trying to output a series of csv files that contain all of the rows that contain a ‘VALUE’ length == 2, ==3 etc. For example: I can get the desired output of one length value
Replace All Specific Characters in String using Python
I have a problem with a function I am trying to implement that needs to replace some letters (in a given string), for some other characters, defined on a dictionary. I have this dictionary: I want to pass a string that when a character matches any key from the dictionary (case insensitive match), it tries to save all the possible
Iterate over 2D numpy array to find corresponding maximum values
I have a set of data as such: I need to iterate over each value in the first column, find the corresponding maximum value in the second column and then store in a new array (or delete the other values from this array). For this example the final output would therefore be: I have managed to write a piece of
How to start a for loop from the end of a vector, and at the value 0 do something
I have to start looping a vector from its end to zero. When I meet the value 0 I need to replace it with the sum of the three previews values. The exception will be for the first zero met, I need just to sum the previous two values. e.g. Expected result: Code The code is not working. Where am
How to iterate through neighbours in 2D coordinates?
In a 2D plane, I want to check the neighbouring points until meeting a condition. For example, take a red pixel in this image (x,y). I want to iterate to find the white pixel closest to the selected red pixel. This is an updated image provided by @Pranav Hosangadi My original code is now irrelevant. A possible solution is to
Fastest way to use if/else statements when looping through dataframe with pandas
I am trying to run conditional statements when iterating through pandas df rows and it results with a very slow code. For example: The df is only about 40k rows long and it’s very slow, as this is only one of the statements I am trying to incorporate with this loop. Can you help with a faster way to do
Why is python printing weird results while using the nested loop here?
I was given an array of strings and I want to split each of the individual strings into separate characters and store them in a separate 2-D array. I wrote the following code:- But doing this gives weird values in my two-dimensional array, mat. Each of the row of the 2D array mat gets printed as [‘1′,’2′,’3′,’4’] but this should
Start and stop command to break a loop
I’m working on a webscraping code, and the process is quite long. Thus, I would like to start and stop it in a fancy way, with a button using tkinter. But I couldn’t find any solution on the web. The .after() function doesn’t seems to be a good option as I don’t want to restart my loop each time. My
I want to extract all the values that a class object holds
I have an object Vm of Type <class ‘azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine’> I want to iterate over this object by a loop so that I don’t have to manually extract the values. Yes I’ve tried isinstance to check whether the value is of type <class ‘azure.mgmt.compute.v2019_07_01.models._models_py3.VirtualMachine’> and extract but in the next iteration it fails. For example let’s consider vm to be an