Save Variables in Every Iteration of for Loop and Load Them Later

save every iteration of loop in a new variable

i found my answer in this post : JavaScript: Dynamically Creating Variables for Loops

i need to create new variables for each iteration . and my code become ...:

for( x = 0; x < res.length; x++){
var results = [];
for( itm in res[x]){
if(result[x] == undefined){
result[x] = "";
}
result[x] += res[x][itm] ;

}
out += "<p>" + result[x] + "</p>";
}


document.innerHTML = out ;

python How do i save a variable from a loop and get it back later

Use a list to save sdroll values on each iteration, this way:

sdroll_list= []
print('Dice script')
for i in range(3):
print('Rolling')
time.sleep(1)
droll = (randint(1, 6))
sdroll = str(droll)
sdroll_list.append(sdroll)
print('Dice rolled '+ sdroll)
time.sleep(3)
add_string = ' '.join(sdroll_list)) #Concatenate the strings in sdroll_list
add_values = sum(map(int, sdroll_list)) #Sum values converted from sdroll_list
print(add)

EDIT:

If you want to sum the values of droll of each iteration, do it as below:

print('Dice script')
sum_droll = 0
for i in range(3):
print('Rolling')
time.sleep(1)
droll = (randint(1, 6))
sum_droll += droll #sum droll values
sdroll = str(droll)
print('Dice rolled '+ sdroll)
time.sleep(3)
print(sum_droll)

Save variables in a loop after reading them out of a .txt file

It looks like you need one more list. This will be list of lists. Something similar to the following:

import numpy as np
import glob

read_files = glob.glob('*.txt')

all_data = []
for i in range(0, len(read_files):
temp = []

txt_file = open(read_files[i], 'r+').readlines()[1:]

for line in txt_file:
line = line.replace(',', '.')
line = line.replace('\t', ' ')
line = line.replace('****', '0')
temp.append(line)

all_data.append(temp)

print(all_data)

store value of for loop iteration into single variable (Python)

You are overwriting words in the loop.

Set it to the empty string before the loop, then use the += operator to concatenate to the variable rather than overwriting the previous values.

words = ''
for i in range(len(ticket_json)):
...
ticketsubject_str = ...
ticketstatus_str = ...
ticketcreationdate = ...
ticketupdateddate = ...
ticketstatusreplace_str = ...
words += (...)


Related Topics



Leave a reply



Submit