for-loop

How to increment a number by 2 in a PHP For Loop

那年仲夏 提交于 2020-01-01 03:52:31
问题 The following is a simplified version of my code: <?php for($n=1; $n<=8; $n++): ?> <p><?php echo $n; ?></p> <p><?php echo $n; ?></p> <?php endfor; ?> I want the loop to run 8 times and I want the number in the first paragraph to increment by 1 with each loop, e.g. 1, 2, 3, 4, 5, 6, 7, 8 (this is obviously simple) However, I want the number in the second paragraph to increment by 2 with each loop, e.g... 1, 3, 5, 7, 9, 11, 13, 15 I can't figure out how to make the number in the second

numpy ufuncs speed vs for loop speed

[亡魂溺海] 提交于 2020-01-01 02:33:09
问题 I've read a lot "avoid for loops with numpy". So, I tried. I was using this code (simplified version). Some auxiliary data: In[1]: import numpy as np resolution = 1000 # this parameter varies tim = np.linspace(-np.pi, np.pi, resolution) prec = np.arange(1, resolution + 1) prec = 2 * prec - 1 values = np.zeros_like(tim) My first implementation was with for loop: In[2]: for i, ti in enumerate(tim): values[i] = np.sum(np.sin(prec * ti)) Then, I got rid of the explicit for cycle, and achieved

PL/SQL Cursor for loop

冷暖自知 提交于 2020-01-01 00:48:36
问题 I believe I need a cursor for loop to go through the street1 column from table test_data. I have a program which needs to test each row from the table. This is what I have so far: cursor c1 is street1 from test_data Begin If Instr(street1, ‘Cnr’, 1) >= 1; Then Newstreetname := Substr(street1, Instr(street1, ‘Cnr’, 1)+3); Else if Instr(street1, ‘PO Box’, 1) >= 1; Then Newstreetname:= Substr(street1, Instr(street1, ‘PO Box’, 1)); Else if REGEXP_ Instr (street1, [\d], 1) = 0; Then Newstreetname:

Issues iterating through JSON list in Python?

*爱你&永不变心* 提交于 2019-12-31 20:37:12
问题 I have a file with JSON data in it, like so: { "Results": [ {"Id": "001", "Name": "Bob", "Items": { "Cars": "1", "Books": "3", "Phones": "1"} }, {"Id": "002", "Name": "Tom", "Items": { "Cars": "1", "Books": "3", "Phones": "1"} }, {"Id": "003", "Name": "Sally", "Items": { "Cars": "1", "Books": "3", "Phones": "1"} }] } I can not figure out how to properly loop through the JSON. I would like to loop through the data and get a Name with the Cars for each member in the dataset. How can I

How does a for each loop guard against an empty list?

无人久伴 提交于 2019-12-31 17:51:13
问题 I read on http://www.leepoint.net/notes-java/flow/loops/foreach.html. the for each equivalent to for (int i = 0; i < arr.length; i++) { type var = arr[i]; body-of-loop } is for (type var : arr) { body-of-loop } My question is how does a for each loop work for an empty list. I know for the regular for loop, the arr.length will just evaluate to 0 and the loop wont execute. What about the for each loop? 回答1: My question is how does a for each loop work for an empty list ForEach also works in the

How do I write a for-loop in Swift 3 for an array that I modify during the for loop?

柔情痞子 提交于 2019-12-31 10:17:25
问题 So, I have a for-loop that looks similar to this: for var i = 0; i < results.count ; i += 1 { if (results[i] < 5) { results.removeAtIndex(i) i -= 1 } } This used to work. But when I changed it to the preferred Swift 3.0 syntax: for var i in 0..<results.count { if (results[i] < 5) { results.removeAtIndex(i) i -= 1 } } I get an array IOOBE exception because it doesn't re-check the count and continues on until the original results.count . How do I fix this? It works now, but I don't want to get

Can I yield from an inner function?

▼魔方 西西 提交于 2019-12-31 08:58:52
问题 With ES6 generators, I see code like this: var trivialGenerator = function *(array) { var i,item; for(var i=0; i < array.length; i++){ item = array[i]; yield item; }; }; Is it possible to write something more like the code below instead? var trivialGenerator = function *(array) { array.forEach(function *(item){ yield item; }); }; I'm asking because the classic for loop is an abomination. 回答1: No, you can't use yield inside of the inner function. But in your case you don't need it. You can

Understanding nested for loops in javascript

[亡魂溺海] 提交于 2019-12-31 08:15:32
问题 I'm learning JavaScript at the moment on freecodecamp and they have an example for nested for loops in one of their excercises: var arr = [[1,2], [3,4], [5,6]]; for (var i=0; i < arr.length; i++) { for (var j=0; j < arr[i].length; j++) { console.log(arr[i][j]); } } With console.log = 1 2 3 4 5 6 undefined. I understand for loops more or less, and I understand that [i] and [j] are used to access the array (I think?). I just don't understand why at the end it just prints out those numbers? I

Sql execution speed very slow

懵懂的女人 提交于 2019-12-31 07:06:49
问题 I have for loop and within that loop I have used INSERT INTO command for 75000+ values. when i am running it consumes more time. how can I improve insertion speed,... thanks in advance... rgs tharindu 回答1: If you are using SQL Server (was not specified): Rather than many individual INSERT calls, use a Bulk method such as SQLBulkCopy BULK INSERT bcp Utility 回答2: If you have a loop with 75k inserts, you're doing it wrong Based on your comments, you need something to gererate rows for you. ;WITH

How can I combine a conditional with a for loop in Python?

天大地大妈咪最大 提交于 2019-12-31 06:30:47
问题 I have a simple example I've drawn up. I thought it was possible to combine if statements and for loops with minimal effort in Python. Given: sublists = [number1, number2, number3] for sublist in sublists: if sublist: print(sublist) I thought I could condense the for loop to: for sublist in sublists if sublist: but this results in invalid syntax. I'm not too particular on this example, I just want a method of one lining simple if statements with loops. 回答1: if you want to filter out all the