loops

Why are for-loops much slower than broadcasting

Why are for-loops much slower than broadcasting Question: Comparing two chunks of code for a simple matrix operation, the one with a nested for loop is much slower. I wonder: what is the underlying reason for this? This loop tuns for 2.5 seconds: m = np.zeros((800,8000)) for i in range(0,800): for j in range(0,8000): m[i,j] …

Total answers: 1

Iterate through a string and append static values to a list for every occurrence of substring (Python)

Iterate through a string and append static values to a list for every occurrence of substring (Python) Question: I’m currently stuck on some basic Python. I currently have a very long html string that looks something like this: <relative-time class="no-wrap" datetime="2023-03-07T02:38:29Z" title="Mar 6, 2023, 7:38 PM MST">Mar 6, 2023</relative-time>, <relative-time data-view-component="true" datetime="2023-03-06T10:25:38-07:00 I want to …

Total answers: 1

How to get only a single output of the result in python program?

How to get only a single output of the result in python program? Question: I was writing a code in python to identify prime number. Here is the code n=int(input("Enter any integer")) for i in range(2,n,1): if n%i==0: print(n, " is not a prime number") break else: print("Prime number") But the problem is that I …

Total answers: 3

Finding minimum iterative

Finding minimum iterative Question: sales_data = { ‘order_number’: [1001, 1002, 1003, 1004, 1005], ‘order_date’: [‘2022-02-01’, ‘2022-02-03’, ‘2022-02-07’, ‘2022-02-10’, ‘2022-02-14’] } sales_data = pd.DataFrame(sales_data) capacity_data = { ‘date’: pd.date_range(start=’2022-02-01′, end=’2022-02-28′, freq=’D’), ‘capacity’: [0, 0, 0, 1, 1, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, …

Total answers: 2

Is there any solution to substitute loops in Numpy?

Is there any solution to substitute loops in Numpy? Question: I now have a simple loop for i in range(4): Q[i, x[i] : x[i] + 2] = 1 / 2 where Q = np.zeros([4,4]) and x = [0,1,0,1]. The goal of this question is to obtain the result of this loop without using loop but …

Total answers: 1

How to loop through 2 columns in a dataframe and replace the value in another column with something else?

How to loop through 2 columns in a dataframe and replace the value in another column with something else? Question: So I need to find replace the value for annual_data[‘countyfips’] from ‘NA’ to 5097 where annual_data [‘BUYER_STATE’] == ‘AR’ and annual_data[‘BUYER_COUNTY’] == ‘MONTGOMERY’. Below is my code: annual_data = annual_data[[‘BUYER_COUNTY’, ‘BUYER_STATE’, ‘year’, ‘DOSAGE_UNIT’, ‘countyfips’]] annual_data …

Total answers: 1

How to iterate through two list in python

How to iterate through two list in python Question: I have 2 list, which i would like to iterate through and capture the selection/input from user. list1 = [‘E1’, ‘E2’, ‘E3’] list2 = [‘api1’, ‘api2’, ‘api3’, ‘api4’] def display_option(options): for env_option, type in enumerate(options,1): print("t{}. {}".format(env_option, type)) def select_option_from_list_of_values(item): while True: print("this option available for …

Total answers: 2

How to loop through variables w/ trend trading system

How to loop through variables w/ trend trading system Question: I built a simple moving average trading system like so: import pandas as pd import numpy as np def trendfunc(f1,fastnum,slownum): f1[‘Change’] = f1[‘Close’].pct_change() f1[‘Fast’] = f1[‘Close’].rolling(window=fastnum,center=False).mean() f1[‘Slow’] = f1[‘Close’].rolling(window=slownum,center=False).mean() f1[‘Trend’] = f1[‘Fast’] – f1[‘Slow’] f1[‘Position’] = np.where(f1[‘Trend’].shift(2)>0,1,0) f1[‘Result’] = f1[‘Position’]*f1[‘Change’] f1 = f1.dropna() f1[‘MktVAMI’] = …

Total answers: 1

Python: Change Order of Lists for Nested Loops

How to change order of lists for nested loops Question: I am trying to write a code to change the order of called lists in nested loops. The code I’m currently using is below. This is a simplified example, so I have removed all the extra functions and documentation. The real code is for creating …

Total answers: 1