for-loop

Why I can't re-assign a variable in a for loop in python? [duplicate]

痞子三分冷 提交于 2020-01-07 09:39:12
问题 This question already has answers here : python assign values to list elements in loop (4 answers) Closed 5 years ago . Today I find an interesting fact, that I can't re-assign variable in a 'for loop', but why? Here is my code: >>> my_list = [1,2,3,4,5] >>> for i in my_list: >>> if i > 3: >>> i = 'my_value' >>> my_list [1, 2, 3, 4, 5] 回答1: When you loop over a list with for i in my_list , i isn't bound to a list cell on each iteration. It's bound to the object the list cell referred to.

JavaScript For loop vs for in does not work

混江龙づ霸主 提交于 2020-01-07 09:35:17
问题 I wrote two functions named some and every to expect to get results shown as below: console.log(every([NaN, NaN, NaN], isNaN)); // → true console.log(every([NaN, NaN, 4], isNaN)); // → false console.log(some([NaN, 3, 4], isNaN)); // → true console.log(some([2, 3, 4], isNaN)); // → false My functions are: function every(array,predicate){ for (var element in array){ if (!predicate(element)) return false; } return true; } function some(array, predicate){ for (var element in array){ if (predicate

Pattern Using for loop

半世苍凉 提交于 2020-01-07 09:35:09
问题 I have to print the following pattern using for loop only. I have written the following code which gives me till the the numbers increment. How can I decrement then in the other half. #include<stdio.h> int main(void) { int x,r; for(r=1;r<5;r++) { for(x=3;x>=r;x--) { printf(" "); } for(x=1;x<=r;x++) { printf("%d",x); } printf("\n"); } return 0; } 回答1: just add a second loop from r-1 to 1? for (x = r - 1; x > 0; x--) { printf("%d",x); } 回答2: Added one more loop for(x=1;x<=(r-1);x++) { printf("

Pattern Using for loop

你离开我真会死。 提交于 2020-01-07 09:35:04
问题 I have to print the following pattern using for loop only. I have written the following code which gives me till the the numbers increment. How can I decrement then in the other half. #include<stdio.h> int main(void) { int x,r; for(r=1;r<5;r++) { for(x=3;x>=r;x--) { printf(" "); } for(x=1;x<=r;x++) { printf("%d",x); } printf("\n"); } return 0; } 回答1: just add a second loop from r-1 to 1? for (x = r - 1; x > 0; x--) { printf("%d",x); } 回答2: Added one more loop for(x=1;x<=(r-1);x++) { printf("

filling an array in a loop

佐手、 提交于 2020-01-07 09:23:23
问题 I have a function that constantly gets called. I have 3 smaller arrays that I need to pack into a larger array each loop float* a; float* b; float* c; float* abc; a = calloc(1, sizeof(float)*3); b = calloc(1, sizeof(float)*3); c = calloc(1, sizeof(float)*3); abc = calloc(1, sizeof(float)*9); void update() { for(int i = 0; i < 3; i++) { fill_a(a); fill_b(b) fill_c(c); abc[0] = a[0]; abc[1] = b[0]; abc[2] = c[0]; abc[0] = a[1]; abc[1] = b[1]; abc[2] = c[1]; abc[0] = a[2]; abc[1] = b[2]; abc[2]

filling an array in a loop

牧云@^-^@ 提交于 2020-01-07 09:23:12
问题 I have a function that constantly gets called. I have 3 smaller arrays that I need to pack into a larger array each loop float* a; float* b; float* c; float* abc; a = calloc(1, sizeof(float)*3); b = calloc(1, sizeof(float)*3); c = calloc(1, sizeof(float)*3); abc = calloc(1, sizeof(float)*9); void update() { for(int i = 0; i < 3; i++) { fill_a(a); fill_b(b) fill_c(c); abc[0] = a[0]; abc[1] = b[0]; abc[2] = c[0]; abc[0] = a[1]; abc[1] = b[1]; abc[2] = c[1]; abc[0] = a[2]; abc[1] = b[2]; abc[2]

javascript closure in loop

时光总嘲笑我的痴心妄想 提交于 2020-01-07 07:10:27
问题 Following code is given: var a = [ ], i = 0, j = 0; for (i = 0; i < 5; i += 1) { (function(c) { a.push(function () { console.log(c); }); })(i); }; for (j = 0; j < 5; j += 1) { a[j](); } Why does i always get bigger by 1 instead of staying 5 ? Hasn't the foor loop already been passed, so the i parameter given to the anonymous function should be 5 ? 回答1: If you referenced i from the inner closure then yes, you would see the result being 5 in all cases. However, you pass i by value to the outer

How to get path relative to the working directory?

坚强是说给别人听的谎言 提交于 2020-01-07 04:59:06
问题 I have a script that recursively loops through all .txt files in the working directory and subdirectories and does something with that files. Now I would like to exclude all files in certain subdirectories that are listed in a exclude.txt file: @ECHO OFF SETLOCAL EnableDelayedExpansion for /r %%f in (*.txt) do ( CALL:processFile %%f %%~df%%~pf ) GOTO:EOF :processFile SET file_=%~1 SET path_=%~2 <- %%~df%%~pf is the full path :( find "!path_!" exclude.txt IF !ERRORLEVEL! EQU 1 ( REM do

Ping function returns that all pinged IP addresses is reachable

走远了吗. 提交于 2020-01-07 04:45:08
问题 I am tring to ping IP addresses from 192.168.1.1 to 192.168.1.254. First I was using I InetAddress class but it was bugged and some IPs where not reachable even if they are. After that I tried this method and it worked very well for single ping IP but when I put it inside for-loop all pinged IPs where reachable... Can you guys tell me what's wrong here? CODE: public class Main { public static void main(String[] args) { String ip="192.168.1."; try { for(int i=0;i<=254;i++){ String ip2=ip+i;

Marking an area with a double 'For' loop

佐手、 提交于 2020-01-07 03:52:13
问题 I am trying to do the following: I want to store different areas (marked in yellow) in a string. The first yellow area is F7:G8, second is I7:J8 and so on, such that the string becomes: "F7:G8,I7:J8,L7:M8,F10:G11,I10:J11, L10:M11". So in this example I have three areas to the right, and two down. The number of areas to the right and downwards may vary, therefore I want to make a code where I only have to specify how many areas to the right and downwards. Note that the first area always is F7