Questions tagged [for-loop]

A for loop is a control structure used by many programming languages to iterate over a range. It is a way of repeating statements a number of times until the loop ends. Depending on the language this may be over a range of integers, iterators, etc.

for-loop
Filter by
Sorted by
Tagged with
0 votes
2 answers
69 views

Why do all arguments except the first one get ignored?

I am trying to make a custom command prompt. For example, if I type in . filename.sh 1 hello 2 hi 3 a 0 b the custom prompt should look like this: [b][hello][hi][a]$ Number 1 stands for second ...
dsv2001's user avatar
1 vote
3 answers
8k views

Make dictionary with for loop in python

I want to create a dictionary in python with 40 key/value pairs. They keys should be a1, a2 ... a10, b1, b2 ... b10, c1, c2 ... c10, d1, d2 ... d10. I have a for loop that takes two variables and ...
Grass's user avatar
  • 13
-2 votes
3 answers
224 views

Python - WHILE loop nested in FOR loop

I was trying to achieve this output: 0;12 1;24 2;36 3;48 4;60 ... but I got this: 0;12 1;12 2;12 3;12 4;12 Here is the code: iter = 0 count = 0 letter = 0 for iter in range(5): while letter ...
ahOw22's user avatar
  • 25
2 votes
3 answers
748 views

Is there a way to add a number of elements to the array in C#?

I'm learning C# and I have created a code to add random numbers to a List using a for loop. class Program { static void Main(string[] args) { Random numberGen = new Random(); ...
Arayik Hovhannisyan's user avatar
0 votes
1 answer
26 views

Bash: more than 1 variable in "For-in" Loop?

example: for i in 1 2 3 4, j in 7 8 9 0 do echo $i"="$j done output: 1=7 2=8 3=9 4=0 i know this code will throw you a lot of error right away, but is there any way?
CuriousNewbie's user avatar
1 vote
2 answers
1k views

prompt user to enter string which is free of special characters [@ . , !] Javascript

In my code the problem is that it validates at first wrong input and then tells the user to enter again but when the user again enters wrong its validation fails and says alert "correct in" Write a ...
bilalmohib's user avatar
0 votes
0 answers
27 views

JavaScript delay in for loop (with p5.js) [duplicate]

How can I get a delay per every draw point in this programm? Btw. i am using p5.js function points_oben() { for (let a = 0; a < koords.length; a++) { setTimeout(function() { for (...
Sebastian Lauss's user avatar
0 votes
1 answer
1k views

FOR-loop does NOT work in Postgres 11.8 when using it in Pl/pgSQL

I'm trying out examples for FOR loops from PostgreSQLTutorial website on this link and this is sample database that's being used. Problem is I can not get to work is the following for-loop example: ...
NikolaS's user avatar
  • 527
0 votes
1 answer
220 views

How to create multiple empty pd.DataFrame which are named like values from List

I want to create multiple empty pd.DataFrame and I thought I can do it with a loop like this: for share in tickers: share=pd.DataFrame() with: tickers=['AAPL', 'MSFT', '^GSPC', 'VNA.DE', '^GDAXI',...
MikeMike's user avatar
3 votes
5 answers
6k views

Finding if a value in a list is greater than the item below it

I've looked for a solution for this for a little while, and while i have snippets of understanding, i can't quite get it to work as expected. I'm hoping i could get some insight: So i have a list ...
Ell Jeff's user avatar
0 votes
6 answers
3k views

How to write a for loop program that finds the sum of all odd numbers in a range, but does not use the if function

I need to write a code that asks for user input 'num' of any number and calculates the sum of all odd numbers in the range of 1 to num. I can't seem to figure out how to write this code, because we ...
ab_410's user avatar
  • 13
0 votes
1 answer
57 views

How to store generate result from a function inside a for loop to DataFrame in Python?

I would like to store generate results from a function that I created called "surface_coverage". The function returns result in numpy.ndarray form and I tried to store it into a dataframe but I kept ...
Ngan Huynh's user avatar
0 votes
3 answers
59 views

Python range and for loops

I was toying around with Python and I ran the following: for i,j in (range(-1, 2, 2),range(-1, 2, 2)): print(i,j) I expected this to print >> -1, -1 >> 1, 1 However what came out ...
BOSSrobot's user avatar
-1 votes
1 answer
395 views

How to loop through a dataframe column name with conditions in Python Pandas to get min value?

I would like to learn how to loop through a columns name with conditions in pandas. For example, I have a list T = [400,500,600]. I have dataframe with columns name are G_ads_400, G_ads_500 ... I ...
Ngan Huynh's user avatar
0 votes
1 answer
55 views

How can I cd into directories in a for loop when the directories have spaces?

Here is a simple for loop where aaa.txt is a list of directories. #!/bin/bash for variable in $(cat aaa.txt) do cd $variable && mv *.avi /home/xxx/Downloads/Marple && rm -rf $...
user203748's user avatar
0 votes
1 answer
284 views

Levels of a Factor into a New List (R)

I currently have a factor adj_factor that has just under a thousand levels, each containing a different adjective . I created this factor from a data frame that contained only one column, an adjective,...
djc1223's user avatar
  • 45
0 votes
1 answer
304 views

Outputting lists as variable name in for loop in R

I have a loop over a function months18 <- list("MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC") for (i in months18) { i <- make_list18(i) } I would like it to output 8 separate lists, each ...
sethparker's user avatar
0 votes
1 answer
68 views

Optimizing the speed of loop in a xts format in R

I have a data structured as xts like bellow: The dimension is 2298 rows and 30 columns. I want to do a loop in each row and column and store it in a new matrix/data frame. Considering the xts data as ...
Agi's user avatar
  • 73
0 votes
2 answers
74 views

Bioinformatics in Python: Is there a less lengthy approach to my code? Has a lot of "if" and "for" statements

def prot_from_rf(aa_seq): prot1 = [] global proteins for aa in aa_seq: if aa == "_": if prot1: for p in prot1: proteins.append(p) ...
Ethan Hetrick's user avatar
0 votes
0 answers
46 views

for-loop does not output with order

I have a df that contains the hi, low, close, EMA200, etc... When I run this for loop I get something really weird: for i in range(100,len(price)): max_idx = list(argrelextrema(hi.values[:i], np....
Stergio Hikaros's user avatar
0 votes
3 answers
201 views

Weird output using for loop to calculate bmi

I'm getting a weird output and I'm not sure why. I'm using google colaboratory. Just can't figure out why is adding the bmi 7 times at the end of each nested list. I only need it added once. I ...
Lex Leach's user avatar
0 votes
1 answer
66 views

Saving Multiple Outputs from a for loop in r

I am trying to use a for loop to multiple create dataframes. The original code works fine for a single run (without the for loop). object<- c(1,2,3) for (i in 1:length(object)) { df1<- ...
Michael Soto's user avatar
1 vote
2 answers
1k views

Creating a list of plots using a for loop

I'm having trouble with creating a list of plots using for loops, and I don't know why. Here's the code that doesn't work (i.e. returns aplotfinal as an empty list) aplotfinal <- list() for(i in ...
Drew 's user avatar
  • 573
0 votes
1 answer
147 views

what is the time and space complexity of this FOR loop?

here alphabets array can be just ['a'] or ['a', 'c'] or all the alphabets -> unique entries, max can only be 26 here words array can be from 1 to many records. no upper bound for(int i=0; i <...
venba's user avatar
  • 37
1 vote
3 answers
722 views

Python nesting elif,for

Is it possible to write a for inside elif? i have many elif statements and i want my last elif statement to have a for inside elif how to write nested elif in PYTHON def victory(see,z): if see[0]==...
curios JS's user avatar
0 votes
3 answers
32 views

Jquery Combine Array of Arrays in another Array of Arrays

I have this Array of Arrays : arr = [[1,a],[1,b],[2,c],[2,d],[3,e],[3,f]]; ...and I would like to transform it into this: arr = [[1,a,b],[2,c,d],[3,e,f]]; I guess it's with a 'for loop', but I don'...
user9626146's user avatar
1 vote
1 answer
155 views

Returning a column to use in for loop for naive-bayes in R

I'm doing a naive-bayes algorithm in R. The main goal is to predict a variable's value. But in this specific task, I'm trying to see which column is better at predicting it. This is an example of what ...
Igna Mendia's user avatar
3 votes
3 answers
646 views

Why does for-each work, but for...of... doesn't?

This is my code to fetch data from firebase: const querySnapshot = await db.collection('entries').get() I want to add the data of every entry-element to a new array for which I got two ways to do it:...
Septias's user avatar
  • 33
0 votes
1 answer
41 views

Create a 3-D Array through a for loop in python

I want to create a 3-D array with a for loop. I tried to create a 2D array with a for loop with the following code: i = 0 wght_prox = np.zeros((250,100), np.float64) for i in range(250): ...
Wayne2's user avatar
  • 23
0 votes
0 answers
55 views

How can I use a for loop into function arguments?

I'm new at programming in R and I'm trying to create a function that uses a for loop in function arguments. Specifically, I want that the function uses two matrices (X and Y) columns as arguments in ...
Juan Vargas's user avatar
0 votes
1 answer
59 views

Nested For Loops locking up Excel

I built a COVID model using UVA data. That data is currently unavailable so I've found another data source. Of course, the new data is not in the same format. So rather than starting from scratch, I'm ...
Chris H.'s user avatar
  • 325
0 votes
1 answer
39 views

For each loop, update variable

I have an assignment where I have to implement code stubs. A simplified version basically is: for a,b in zip(var_a,var_b): # implementation here I'm pretty sure the task is basically to update a ...
sebko_iic's user avatar
  • 340
0 votes
0 answers
34 views

For loop to copy cells from one sheet to another works but gives an error at the end of the loop

I have wrote a simple For code which is actually working but after it gives me all the values i want, it goes on error "run-time-13 type not matching". I am copying the code in the hope that a more ...
Alessandro 's user avatar
0 votes
2 answers
530 views

Solving equation with for loops python

I have arrays like this: x = np.array([-1,-1,-1,1,-1,-1]) weight = np.array([[0.5,-0.5,0.5,-0.5,0.5,-0.5], [-0.5,0.5,-0.5,0.5,-0.5,0.5], [0.5,0.5,0.5,0.5,0.5,0....
lighting's user avatar
  • 400
1 vote
1 answer
711 views

How to make for loop run in parallel in tensorflow

I've been looking around everywhere for this extremely simple question without any luck. I have the following code: @tf.function def parTest(): for i in tf.range(tf.constant(10)): ...
Beacon of Wierd's user avatar
1 vote
2 answers
227 views

C# how to remove last line from a loop

i wrote a code that is working fine. however there is something annoying me. Here is the code string secretWord = "ola"; string guess; int counter = 4; Console....
Iuri De Barros's user avatar
1 vote
0 answers
172 views

How do I insert millions of records with speed and at once into an sql table using vb.net?

I have am filling gridview with data and insert record 1 by 1 into an sql table using a stored procedure which has only insert statement. This is ok but when records are in millions then this takes ...
Stacky's user avatar
  • 29
0 votes
1 answer
109 views

Automatically calculating percentage and store into variables

I am currently on a demographic project. I have data from 3 different countries and their birth statistics in each month. My question: I want to calculate the percentage people born in each month and ...
alexryder's user avatar
1 vote
2 answers
62 views

Trying to use for loop to shorten js but getting "not defined" error

I'm a beginner in javascript and I'm trying to improve this: (which has been tested and works fine) // snail 1 // var s1 = document.createElement("div"); s1.id = snail1.id; s1.className = "snail-...
KHS's user avatar
  • 7
0 votes
1 answer
1k views

Modulo in order to get single int from long

C language question. My task is to get second to the last, third to the last digit etc. from a long. Got a hint to do it by using modulo operator: int main(void) { a = get_long("n:\n"); ...
piotrektnw's user avatar
3 votes
5 answers
7k views

Iterate through list and check if I am at end of list

I have a list of strings: animals = ["cat meow ", "dog bark"] And I want to check if each string contains the word "cow" which obviously does not exist in my above list. I am trying to write an if ...
Vasilis Skentos's user avatar
0 votes
0 answers
31 views

how many green fly could there be by the end of day 28? Please check the program if it is the correct solution

Green y can reproduce asexually. After one week of life a lone female can produce eight ospring a day. Starting at the beginning of day 1 with a single mature female, how many green y could there be ...
Amrit Khadka's user avatar
1 vote
1 answer
210 views

LUA script: Nested loop inserts only last item

I have two loops, a main loop and a sub-loop inside the main loop. Both loops populate the same table (and sub-table). But for some reason the sub-loop only stores the last added item in the nested ...
Anton Lundberg's user avatar
0 votes
2 answers
51 views

Single line for loop with two returns in python 3

I would like to return two values from a single line for loop in brackets. x,y = [xval, yval for xval, yval in zip(x,y) if condition] This gives me a syntax error. I am only able to run the this ...
M. St.'s user avatar
  • 63
3 votes
1 answer
2k views

Why is for loops so slow in tensorflow

So I know this has something to do with when tensorflow builds the graph and it doesn't do it well... "efficiently". Here's the dummy code I'm running: @tf.function def parTest(x_in): res = 0 ...
Beacon of Wierd's user avatar
8 votes
1 answer
2k views

How to implement a decrementing for loop in Julia?

I know that in python I can do something as follows. for i in range(10, 0, -1): print(i) Which will output: 10 9 8 7 6 5 4 3 2 1 I'm very much new to julia and I know I can create normal loops ...
Bilesh Ganguly's user avatar
0 votes
0 answers
258 views

How to efficiently loop through same data frame twice in pandas with breaking upon reaching a condition?

I have to loop a data frame twice with two pointers where the first pointer starts from 0 to len(df) and the second pointer starts from the first pointer to len(df). It also breaks when a certain ...
Ricky's user avatar
  • 2,700
-1 votes
1 answer
27 views

How do I loop two array as one in PHP

I have a probrem but I don't know how to fix it, here is two array first is$X = A,B,C,D second likes this a[1]a[2]a[3]a[4]a[5]a[6]a[7]a[8] I want to loop likes this Aa[1]a[2] Ba[3]a[4] Ca[5]a[6]...
xuansssss's user avatar
0 votes
1 answer
106 views

Issues in for loop for finding Prime number in C, Visual Studio [closed]

I wrote a simple for loop in C to find if entered number is a prime number. Upon running, even when entering simple values like 7, 13, etc. the programme just sits on it and seems to be processing ...
An Ant's user avatar
  • 194
0 votes
2 answers
3k views

AttributeError: 'tuple' object has no attribute 'value' PYTHON TO EXCEL

I am currently trying to write data to an excel spreadsheet using Python3 and openpyxl. I have figured out how to do it when assigning one single value, but for some reason when I introduce a For loop ...
isaiahoptics22's user avatar

1
421 422
423
424 425
1441