for-loop

Python matrix input not working this way?

Python matrix input not working this way? Question: I have tried several ways that are working. But, this one is not. Please help me understand why ? row, col = 2, 3 mat1 = [[None]*col]*row print(mat1) for i in range(0, row): for j in range(0, col): mat1[i][j] = int(input()) print(mat1) Input: 1 2 3 4 …

Total answers: 2

Avoiding for-loop in NumPy 1D nearest neighbors

Avoiding for-loop in NumPy 1D nearest neighbors Question: I have the following code in which I get the N nearest neighbors in 1D: import numpy as np def find_nnearest(arr, val, N): idxs = [] for v in val: idx = np.abs(arr – v).argsort()[:N] idxs.append(idx) return np.array(idxs) A = np.arange(10, 20) test = find_nnearest(A, A, 3) …

Total answers: 1

Expand list of tuples

Expand list of tuples Question: I have a big list of tuples (20-30 million entries). Each sub-tuple has three pieces of information (start, end, value) and is supposed to only represent one position in a sequence. However, the program generating this data has concatenated some of the values of adjacent positions if the value is …

Total answers: 2

How to visit all points in a 2D grid?

How to visit all points in a 2D grid? Question: I am trying to have 2 points visit all points in a 3 x 3 grid. The method of visiting the points is: I have points P1 and P2, starting from (0, 0). P1 moves from (0, 0) to (0, 1), (1, 0) until (3, …

Total answers: 1

Need to write a function that returns all of the vowels in a word

Need to write a function that returns all of the vowels in a word Question: I am new to learning python and am struggling to create a Function to return a string only of vowels from word no matter if its a blank string, string with no vowels, or string with all vowels. This is …

Total answers: 5

Importing multiple .cnv files, extract filename, and attach as a column

Importing multiple .cnv files, extract filename, and attach as a column Question: I have multiple .cnv files that I can import and concatenate with the following code: import pandas as pd import numpy as np import matplotlib.pyplot as plt import glob # Get CSV files list from a folder path =’/Users/mariacristinaalvarez/Documents/High3/’ csv_files = glob.glob(path + …

Total answers: 2

for Loop multiple conditions

for Loop multiple conditions Question: I am trying to create a new variable (column) which values depend on the values of other variable, therefore I try to create a for loop with multiple conditions. Here is the code that I tried: ABC_Levels = [] for i in range(len(df_ABC)): if df_ABC.loc[i,’ABC’]>=5.7: ABC_Levels.append(‘High’) elif df_ABC.loc[i,’ABC’]<5.4: ABC_Levels.append(‘Low’) else: …

Total answers: 1

Trying to use nested for loop to correctly parse nested list items

Trying to use nested for loop to correctly parse nested list items Question: I have a nested list (derived from a psycopg2 psql query) that contains a list of company names, stock tickers, and industries. I am using a subsequent nested for loop to try and extract the 1st (company name) and 3rd (industry) elements …

Total answers: 1