# 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 …

## 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) …

## 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 …

## 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, …

## 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 …

## Creating variables from a python dictionary with multiple values for each key using a for loop

Creating variables from a python dictionary with multiple values for each key using a for loop Question: I have an audio (wav) file imported into Python using the pydub package. I have a dictionary consisting of each key matching to 2 values. The key is meant to be used as the name for the variable …

## Is there any way to loop the second list multiple times to properly riffle two lists in Python similar to the Mathematica function?

Is there any way to loop the second list multiple times to properly riffle two lists in Python similar to the Mathematica function? Question: I’m trying to convert the Mathematica built-in function, "Riffle" into python. Right now, my main focus is getting it to riffle two lists properly (I will worry about adding values at …

## 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 + …