for-loop

How to loop through a collection that supports IEnumerable?

北战南征 提交于 2019-12-29 13:35:39
问题 How to loop through a collection that supports IEnumerable? 回答1: A regular for each will do: foreach (var item in collection) { // do your stuff } 回答2: Along with the already suggested methods of using a foreach loop, I thought I'd also mention that any object that implements IEnumerable also provides an IEnumerator interface via the GetEnumerator method. Although this method is usually not necessary, this can be used for manually iterating over collections, and is particularly useful when

Pythonic iteration over multiple lists in parallel

感情迁移 提交于 2019-12-29 09:29:23
问题 I'd like to know an elegant, Pythonic way to iterate over a list of lists (or dict of lists) in parallel in Python 3. The number of lists is not known until runtime, so I believe I cannot simply supply them as arguments to the zip() function. For example, given the following data structure: var = [['x1' ,'x2' ,'x3'], ['y1', 'y2', 'y3'], ['z1', 'z2', 'z3'], …] I would like to be able to access the following values on each iteration: x1,y1,z1 followed by x2,y2,z2 followed by x3,y3,z3 and so on.

Elixir: Return value from for loop

隐身守侯 提交于 2019-12-29 09:21:55
问题 I have a requirement for a for loop in Elixir that returns a calculated value. Here is my simple example: a = 0 for i <- 1..10 do a = a + 1 IO.inspect a end IO.inspect a Here is the output: warning: variable i is unused Untitled 15:2 2 2 2 2 2 2 2 2 2 2 1 I know that i is unused and can be used in place of a in this example, but that's not the question. The question is how do you get the for loop to return the variable a = 10? 回答1: You cannot do it this way as variables in Elixir are

Infinite loop application - for(;;)

醉酒当歌 提交于 2019-12-29 09:19:11
问题 I am trying to understand the concept behind the for loop example for (;;) { //write code } I understand what it does and how it's the same looping structure as while(true), but my question is...is this good programming practice and for what sort of applications would this type of looping structure be applied to? 回答1: There are a few situations when this is desired behavior. For example, the games on cartridge-based game consoles typically have no exit condition in their main loop, as there

Multiple loop variables Matlab

青春壹個敷衍的年華 提交于 2019-12-29 09:05:16
问题 In C++/C , we have multiple loop variables in a single loop, like for(int i=0; int j=0; i<5; j<5; i++; j++) Is there any facility in Matlab for multiple variables loop? And also, I'm very conscious in loop iterations computations , so does it effects the speed as I'v already a nested loop in Matlab . 回答1: MATLAB sort of supports multiple loop variables in that it supports a matrix as the loop expression. How does that work? Individual columns of the matrix are assigned to the loop variable at

How to create an array of non repeating random numbers

送分小仙女□ 提交于 2019-12-29 08:41:32
问题 I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks Here is my code so far: class Lottery { static int[] numberHolder; //array to be filled with numbers up to an //amount entered by the user eg 42 Max static int[]

How to create an array of non repeating random numbers

守給你的承諾、 提交于 2019-12-29 08:41:29
问题 I have a lottery application in C# which takes in the number of numbers to draw and also the maximum number to draw.I have coded up to creating an array holding the required random numbers but I need them to be unique and am having trouble doing it.I would be very grateful if someone could give me some advice on this,Thanks Here is my code so far: class Lottery { static int[] numberHolder; //array to be filled with numbers up to an //amount entered by the user eg 42 Max static int[]

How to fire a callback function after a for-loop is done in Jquery?

时光毁灭记忆、已成空白 提交于 2019-12-29 08:13:31
问题 I have two functions that caluclate and adjust the height and width of elements on screen. panelHeight sets the height of target elements to the available screen height, while panelWidth adjusts the width of elements. My problem: I cannot ensure the second functions ( panelWidth ) fires AFTER the first function ( panelHeight ) is done. If the target element is long and has a scrollbar, it will be removed by panelHeight but if this is not done before panelWidth fires, the set width will be off

Alternative to nesting for loops in Python

两盒软妹~` 提交于 2019-12-29 08:08:06
问题 I've read that one of the key beliefs of Python is that flat > nested. However, if I have several variables counting up, what is the alternative to multiple for loops? My code is for counting grid sums and goes as follows: def horizontal(): for x in range(20): for y in range(17): temp = grid[x][y: y + 4] sum = 0 for n in temp: sum += int(n) print sum # EDIT: the return instead of print was a mistype This seems to me like it is too heavily nested. Firstly, what is considered to many nested

C++11: Range-looping vector from the second element?

主宰稳场 提交于 2019-12-29 07:42:18
问题 I have a std::vector<std::string> v; (initialized). How can I use the range-for loop for accessing all elements except the first one (on index zero). For all elements: for (const string & s: v) process(s); Instead of the v a range expression can be used. How can I write the range expression to skip the first element (or skip the first n elements) ? I know how to get the effect using v.begin() + 1 and using the classic loop. I am searching for the new, more readable, recommended alternative to