Questions tagged [loops]
Loops are a type of control flow structure in programming in which a series of statements may be executed repeatedly until some condition is met.
90,278
questions
0
votes
0
answers
7
views
Changing code to run multiple linear regression models between responses and 8 predictors and saving table as one output
I am unsure of how to alter my code for running linear regression models, which is currently utilising one independent variable at a time and 8 predictors, and saving the effect size, std.error and p ...
0
votes
0
answers
8
views
Reading files from Different folder in R for loop
I am writing a script in R and i am trying to retrieve files from different folder in for loop.
i am doing something like this but didn't work. How can i read files in for loop from different folders?
...
-4
votes
0
answers
54
views
Cache a conditional in for loop [closed]
def fn(x, key=5):
bools = bools_all[key]
for i in range(32):
if bools[i]:
x[i] += 1
else:
x[i] *= 2
TL;DR can above be cached for every executed key?
...
1
vote
1
answer
16
views
Appending tuples to a Pandas dataframe and "cannot concatenate" - what am I doing wrong?
I want to fill up a dataframe from a scraping loop. Say I created an empty DF with four columns.
df = pd.DataFrame(columns=['A','B','C','D'])
Then the loop to fill it looks like this:
for i in range:
...
0
votes
1
answer
21
views
Loop Lists - different methods but same results? [duplicate]
I've started to learn to code with Python and I've just come across Loop Lists. They've given two ways to loop and I wondered when and how example A would be better than just using example B (which is ...
0
votes
0
answers
12
views
Comparing datatable content and counting differences
Every day, I receive a spreadsheet that has the following form:
STATE
Probable
Confirmed
Total
Deaths
There are no missing cells. The maximum number of rows is 60 (includes territories) and maybe 10 ...
-2
votes
0
answers
20
views
The value phii upgrade in the loop
Please help
for r in range(50):
#print(r)
for i in range(1,npy):
for j in range(1,npx):
#print(i,j)
#Se elimina la densidad;ya que en toda la ecuacion
...
0
votes
0
answers
36
views
why would same code produce a different result? [duplicate]
why is this loop given a different result in python. please explain
a, b = 0, 1
while a < 50:
a, b = b, a + b
print(a)
this below code is the same as the one above except that the ...
0
votes
4
answers
30
views
How to iterate over list values in dictionary per key?
Here's my code:
msgs = {}
msgs['ch1']=['testmsg1', 'testmsg2']
msgs['ch2']=['testmsg3','testmsg4','testmsg5']
counter = 0
for key, val in msgs.items():
while counter < len(val):
print(...
0
votes
1
answer
12
views
How do you use a loop to populate an array and search the array for a specific value in Simple pseudocode
How do you write the pseudocode to declare and populate an array with 10 elements. I need to use a loop to populate the array and search the array for the value 20. If the value is found, display a ...
0
votes
0
answers
19
views
Batch updating an SQL table by month with multiple WHEREs
I have a query where I am uncommenting and re-commenting in multiple WHERE clauses since I would like to update a table one month at a time. See below sample WHERE clause:
update L
set
[...
0
votes
0
answers
8
views
daily Google Trends data for a longer period
I want to download google trends data for "gold prices" in the US for the period of 1-1-2004 to 31-12-2019. How can I do it with a loop in R?
library(gtrendsR)
I have created a vector of ...
0
votes
2
answers
44
views
For loop only shows last value in array
I'm trying to list out a set of values, that only show the new updated values in an array, but unfortunately it is only printing out the last value in the array. How do I show all the values and not ...
0
votes
3
answers
50
views
Python looping list and append value to variable
I have a .csv file with IPs which I converted into a list with Python:
def ip_list():
iplist = []
with open("/path/to/file") as csvfile:
csvlist = csv.reader(csvfile)
...
0
votes
2
answers
49
views
Populate array by recycling a smaller array starting with a particular value
I need to populate an array with a predefined number of elements ($vacancies) using a pre-existing array of values ($list_order_id).
The source array may not contain enough values to meet the required ...
-5
votes
0
answers
24
views
Python double for loop given recursion [closed]
I want to write a double for loop (a for loop nested within a for loop) that calculates the following approximation of the natural logarithm of x.
The following is given:
(I'm posting pictures ...
0
votes
0
answers
18
views
Combining the data of 3 tables
I am working on a database for my neuronal network. Therefore I need to combine the data of 3 different tables. The information in those tables is as follows.
Table 1: name of picture, additional data(...
1
vote
1
answer
18
views
Python Dataframes Looping over a counter field
I would like to build a loop where the function will loop over the "counter" field creating subset dataframes and then join them back together
The below is a small example of what i want, in ...
0
votes
2
answers
58
views
What's the mathematical formula for a nested loop?
Imagine this loop:
for i in range(3):
for j in range(3):
for k in range(3):
print("{}{}{}".format(letter, number, symbol))
I want to generate this sequence numerically - without a ...
0
votes
0
answers
21
views
Assign symbolic link to an array of files
(preface: I'm brand new to coding!)
I have a folder full of files, which I need to assign symbolic links to a set within those files.
Currently I am able to to create my list of files I want to use:
...
0
votes
2
answers
43
views
How to use loop in an if else statement in C Programming
I need to construct an algorithm that reads the membership type and item price.
The output will be the amount to pay after discount.
The program allows both lowercase and uppercase character input and ...
0
votes
1
answer
31
views
Speed up large nested loop with DataTable and FileInfo
I have a DataTable which contains approximately 150.000 rows. I try to compare those rows to Files (approximately 200.000 files) in a specific folder. My code looks like:
foreach (DataRow row in ...
0
votes
0
answers
57
views
Why program skips scanf()? c++ [closed]
When in the first iteration of loop I enter some letter or symbol, it recognises it as a invalid input for scanf(), but in the next iteration it just skips the scanf() and just function as infinite ...
0
votes
1
answer
38
views
Grouping a mass spectrometry data in R by loop
I want to make group of every 3 columns in different group in the data frame. I have attached the example data frame image. It is having 12 columns.
Example data frame
Based on the pattern I have to ...
-2
votes
4
answers
39
views
for loop , trying to print all element with index
why following code is giving the error
TypeError Traceback (most recent call last)
<ipython-input-14-31d3f85500ea> in <module>
1 name = "Shyam&...
-1
votes
1
answer
36
views
For C++: Having some issues with my while loop
//Im trying to encrypt and decrypt in my program however, my while loop for repeating the program once it is executed isn't working. I've tried adding a break statement to separate the input responses,...
2
votes
4
answers
73
views
Python: loop and edit string character
I'm new to python and coding in general. Here is a problem from my homework, as well as my current code. I know that I only have one of the parts, but I wanted to figure this one out before moving on ...
0
votes
0
answers
39
views
Trying to change object property within loop only once
I am trying to create a game where you connect similar colored spheres together to pop for a point amount, then is replaced by a new color. You connect spheres by clicking and holding then dragging to ...
-3
votes
1
answer
36
views
How to take 'snapshot' of variable value before a for loop? C# [closed]
static void Main(string[] args)
{
Random rnd = new Random();
float myfloat = rnd.Next(1, 50);
for (int i = 0; i <= 2; i++)
{
myfloat = 3;
}...
0
votes
0
answers
12
views
folium markers/popups from data with duplicated lat/long
I am attempting make a simple folium map that will plot markers based on given lat/long as well as popups with data about the point.
an example dataframe is like the below:
d = {'Location code': [132, ...
-1
votes
0
answers
12
views
how can i Loop it to beginning in phyton
Hello So i wanna ask some two question again in here cause i have tried a lot of method but did not work first question is how can i make nname variable to enter only characters numbers or any other ...
1
vote
1
answer
34
views
Use a loop/automation for html web scraping
I am performing web-scraping in R (using rvest) for a specific set of data on various webpages. All of the webpages are formatted the same, so I can extract the targeted data from its placement on ...
0
votes
1
answer
35
views
LOOP in R: Error: variable lengths differ
I tried to build this Loop so that I can test two outcomes at the same time. However, it produced an error message: "Error in model.frame.default(formula = ~outcome + centered.predictor1 + : ...
1
vote
1
answer
31
views
How to use a list in a when conditional in Ansible playbook
I'm trying to check if certain mount points have been added but only want to see those I added which are stored into added_mounts.
How to feed a list which is in a variable to a when conditional as in ...
0
votes
1
answer
31
views
bs4 - AttributeError: 'NoneType' object has no attribute 'find_all' when search has no results
I am using Beautifulsoup to filter data from a website. To do this, I pass several search terms in a loop using the site's built-in search box.
If the search term does not find any content, the ...
0
votes
1
answer
15
views
Python Qt5. Using loops to call the QPushButtons
I'm a junior QT user. Trying to understand how it works.
I have multiple QPushButtons. They were created in a QT Designer.
self.K2o = QtWidgets.QPushButton(self.gridLayoutWidget)
self.K2o....
-2
votes
1
answer
33
views
How do I stop a while loop from crashing the emulator in android studio? [closed]
I'm making a time and date thing, and I need to update automatically so I'm using a while loop. Unfortunately, it keeps crashing the emulator. I think this is due to a stack overflow, but I'm not sure....
0
votes
2
answers
45
views
How do you return a specific JavaScript object?
The file is as such
export const countries = [
{
alpha2: "TW",
alpha3: "TWN",
country: "Taiwan",
fifa: "TPE",
},
{
alpha2: "AF&...
0
votes
2
answers
50
views
How to fill an array when the data does not exist?
I have a request that search the "Accommodations" and "Catering" services present on contracts. This query makes a sum per year. Everything works well ! The retrieved data is ...
0
votes
1
answer
58
views
C# Loop inside a loop : Index was outside the bounds of the array
I am getting the error "Index was outside the bounds of the array."
This is what I want to have :
check if the log line contain "ERROR" then check if the bottom line
contain "...
0
votes
0
answers
33
views
R loops: how to adapt multiple columns at once
When analysing a questionnaire, I have come across the following problem. I would like to use a loop to unite two columns from a dataset and this multiple times. For instance:
#This is some data to ...
0
votes
0
answers
17
views
Looping trough list of lists and safe it as seperate df
I created a loop which takes the values from a df when a certain value is met in a specific column.
I safe the remaining column information in a list.
now i want to loop trough a list containing the ...
0
votes
1
answer
20
views
Create intervals in R
I want to create intervals in R, I already got some code to get specific numbers.
For example the following:
"33" "45" "77" "82" "85"
As the result I ...
1
vote
2
answers
30
views
how to access and collect elements from a list in list?
This question is so simple, but the desired output is not clicking to me.
alist = [[0., 0., 0], [1 , 2, 3 ], [4, 5, 6 ],[7, 8, 9], [10, 11, 12], [13, 14, 15]]
a = []
for i in alist:
for j in i:
...
0
votes
2
answers
26
views
Select specific rows and columns - loop - dataframe
I have a dataframe similar to this:
import pandas as pd
data = {'product_name': ['2000', '2001', '2000', '2001', '2002', '2002', '2001', '2000'],
'price': [1200, 150, 300, 450, 200, 300, 450, ...
0
votes
0
answers
16
views
Parent table / Child table declaration
I am working on authomatized way of creating a JSON file with metadata. The said JSON file will be used for synthetic data creation project
I am struggling to find a proper way to declare a ...
0
votes
0
answers
18
views
Avoid a for loop creating a time series with "if" statements [duplicate]
I would like to avoid using a loop creating a data series in a self-referential dataframe. My code is something like this:
data = pd.Dataframe()
data["data1"] = pd.Series(some_info)
data[&...
0
votes
2
answers
16
views
Group a string variable/create categorical variable
I am struggling to find a smooth way in Stata to create a categorical variable from my string variable.
My string variable is called BRANSCH and contains values for different sectors (see picture)
...
0
votes
1
answer
46
views
Python - for loop iteration
I'm trying to build the following function, which contains a for loop.
As you can see a and g are increased by each iteration.
I want to limit the number of iterations to n times. n is a variable in ...
-2
votes
1
answer
58
views
Dynamically check difference between elements in a list
I have a list containing a set of numbers in ascending order.
I have another set of numbers containing a few elements.
I need to check the difference between elements in a list such that they are ...