for-loop

“entirerow.delete” skips entries in For loop

别来无恙 提交于 2020-01-03 02:32:06
问题 I'm trying to clean up a set of data and noticed something strange with vba function entirerow.delete The following code will, as intended, delete the entire row if it is in the format strikethrough, but will skip the rows immediately following it, if they are also in that format. It seems like a it takes a row that is not in the strikethrough format to "reset" the ability to delete more rows. Does anyone know why, or what I could do to debug this? For Each rng In rng1 'Check each character

Matlab: how can I perform row operations without brute-force for loop?

不羁的心 提交于 2020-01-03 02:27:15
问题 I need to do function that works like this : N1 = size(X,1); N2 = size(Xtrain,1); Dist = zeros(N1,N2); for i=1:N1 for j=1:N2 Dist(i,j)=D-sum(X(i,:)==Xtrain(j,:)); end end (X and Xtrain are sparse logical matrixes) It works fine and passes the tests, but I believe it's not very optimal and well-written solution. How can I improve that function using some built Matlab functions? I'm absolutely new to Matlab, so I don't know if there really is an opportunity to make it better somehow. 回答1: You

Running TestNG programmatically need loop to create multiple tests automatically

瘦欲@ 提交于 2020-01-02 21:57:30
问题 I was wondering if someone can give me the right direction for this. Not sure if I'm using Map or HashMap correctly for loop. I know it works if I do one parameters but I like to do 1-100 host parameters when I create my XML programmatically. Can you show me what I need to do for creating a loop so I can create multiple test with parameter with host value 1-100. My code is as below: package firsttestngpackage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import

Enhanced for loop does not work with Scanner inside loop body

故事扮演 提交于 2020-01-02 17:51:39
问题 Why does think not work? It just prints zeros. However it works when I use a normal for loop with an index value 'i' and using 'a[i]' inside the body of the loop. The problem is not with the printing loop, as it does not print the values, even with a normal for loop. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] a = new int[5]; for (int i : a) { System.out.println("Enter number : "); i=s.nextInt(); } System.out

Java Enhanced-For-Loop FASTER than traditional?

岁酱吖の 提交于 2020-01-02 17:33:09
问题 So my understanding was that enhanced for loops should be slower because they must use an Iterator.. However my code is providing mixed results.. (Yes I know that loop logic takes up the majority of time spent in a loop) For a low number of iterations (100-1000), the enhanced for loop seems to be much faster with and without JIT. On the contrary with a high number of iterations (100000000), the traditional loop is much faster. What's going on here? public class NewMain { public static void

How to remove an item while iterating over a list?

我怕爱的太早我们不能终老 提交于 2020-01-02 16:09:12
问题 My code looks like this: def nue(li): for i in li: li.remove(i) print(li) li = [1, 2, 3] nue(li) However, running this results in: >>> [2] More generally, how do I remove the i-th position on a list while iterating over the list (for reasons it failed some test in a nested loop or something like that)? 回答1: you can do like this def nue(li): for i in li[:]: li.remove(i) print(li) li = [1, 2, 3] nue(li) li[:] will clone original li 回答2: Try list comprehension: def remi(slist, i): # remi means

R - how can I save the value of “print”?

佐手、 提交于 2020-01-02 14:01:08
问题 In R , when I use "print", I can see all the values, but how can I save this as a vector? For example, in a 'for' loop: for(i in 1:10) , I would like the value of A , when i= 1,2,3,4..... but if I use x=A , it only saves the final value of A which is the value when i = 10. So, how can I save the values in print(A)? Additionally, I use more than one 'for' loop e.g.: for (i in 1:9) { for (k in 1:4) { } } Consequently, x[i]=A..does not work very well here. 回答1: I think Etiennebr's answer shows

Why is numpy slower than for loop

天大地大妈咪最大 提交于 2020-01-02 13:26:26
问题 I have a function using some for loops and I wanted to improve the speed using numpy. But this seems not to do the trick as the bumpy version appears to be 2 times slower. Here is the code: import numpy as np import itertools import timeit def func(): sample = np.random.random_sample((100, 2)) disc1 = 0 disc2 = 0 n_sample = len(sample) dim = sample.shape[1] for i in range(n_sample): prod = 1 for k in range(dim): sub = np.abs(sample[i, k] - 0.5) prod *= 1 + 0.5 * sub - 0.5 * sub ** 2 disc1 +=

Passing argument to setTimeout in a for loop

China☆狼群 提交于 2020-01-02 11:14:08
问题 I'm trying to learn how to pass an argument to setTimeout in a javacript for loop. Here is the example code. As it is currently written, setTimeout is passed the same exact i each time, not reflecting the different i's that are actually in the array. var a=100; for (i in array) { setTimeout("do_stuff(i, a)"), 2000); } (I've seen somewhat similar questions and answers here, but the code examples are much more complicated. Answering this most basic example could much help others with the same

With JavaScript how would I increment numbers in an array using a for loop?

安稳与你 提交于 2020-01-02 06:24:10
问题 I am trying to increment the numbers in the array var myArray = [1, 2, 3, 4]; I try to use for (var i = 0; i < myArray.length; i++){ myArray[i] + 1; } but that doesn't seem to do anything :( please help 回答1: You can use map() which will make it quite clean: var arr = [1,2,3,4]; arr = arr.map(function(val){return ++val;}); console.log(arr); 回答2: There's many possibilities to do that, you can use plus equal += like following : for (var i = 0; i < myArray.length; i++){ myArray[i] += 1; } Or