for-loop

push() a two-dimensional array

有些话、适合烂在心里 提交于 2019-12-28 05:49:05
问题 I'm trying to push to a two-dimensional array without it messing up, currently My array is: var myArray = [ [1,1,1,1,1], [1,1,1,1,1], [1,1,1,1,1] ] And my code I'm trying is: var r = 3; //start from rows 3 var c = 5; //start from col 5 var rows = 8; var cols = 7; for (var i = r; i < rows; i++) { for (var j = c; j < cols; j++) { myArray[i][j].push(0); } } That should result in the following: var myArray = [ [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [1,1,1,1,1,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0

Python - Way to restart a for loop, similar to “continue” for while loops? [duplicate]

*爱你&永不变心* 提交于 2019-12-28 04:15:05
问题 This question already has answers here : python: restarting a loop (5 answers) Closed 3 years ago . Basically, I need a way to return control to the beginning of a for loop and actually restart the entire iteration process after taking an action if a certain condition is met. What I'm trying to do is this: for index, item in enumerate(list2): if item == '||' and list2[index-1] == '||': del list2[index] *<some action that resarts the whole process>* That way, if ['berry','||','||','||',

Matlab: arrayfun, cellfun, spfun and structfun vs. simple for-loop

岁酱吖の 提交于 2019-12-28 04:09:04
问题 Which one is better, using all the *fun functions ( arrayfun , cellfun , structfun and spfun ) or simply using for loop? What method gives better performance and which methods should be considered better practice in terms of readability of the code? 回答1: It really depends on what you call 'performance' :) If you mean minimum execution time , well, sometimes *fun are faster (for example, cellfun('isempty', ...); (yes, string argument!) for sure beats the loop version). Sometimes a loop is

While, Do While, For loops in Assembly Language (emu8086)

自作多情 提交于 2019-12-28 04:03:44
问题 I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code: for(int x = 0; x<=3; x++) { //Do something! } or int x=1; do{ //Do something! } while(x==1) or while(x==1){ //Do something } How do I do this in emu8086? 回答1: For-loops: For-loop in C: for(int x = 0; x<=3; x++) { //Do something! } The same loop in 8086 assembler: xor cx,cx ; cx-register is the counter, set to 0 loop1 nop ; Whatever you wanna do goes here, should not change cx inc

Why is int rather than unsigned int used for C and C++ for loops?

大憨熊 提交于 2019-12-28 03:42:09
问题 This is a rather silly question but why is int commonly used instead of unsigned int when defining a for loop for an array in C or C++? for(int i;i<arraySize;i++){} for(unsigned int i;i<arraySize;i++){} I recognize the benefits of using int when doing something other than array indexing and the benefits of an iterator when using C++ containers. Is it just because it does not matter when looping through an array? Or should I avoid it all together and use a different type such as size_t ? 回答1:

Syntax error: “(” unexpected — with !(*.sh) in bash script

人走茶凉 提交于 2019-12-28 03:14:37
问题 I want to run a sh file: #!/bin/bash for f in !(*.sh); do ffmpeg -i "$f" -vf yadif=0:-1 -threads 0 -c:v libx264 -pix_fmt yuv420p \ -r 29.97 -b:v 3000k -s 1280x720 -preset:v slow -profile:v Main \ -level 3.1 -bf 2 -movflags faststart /mnt/media/out-mp4/"${f%.mxf}.mp4" rm $f done However, I get the following error: 2: task1.sh: Syntax error: "(" unexpected If I try directly on the command line it works perfectly. the path and permissions are already reviewed Any idea what might be happening?

What does a for loop without curly braces do?

不想你离开。 提交于 2019-12-28 03:10:09
问题 Hi so basically my question is what does a for loop without any curly braces around it do? So from what I know, that during an if-statement only the first line of the code is executed. So in a for loop how does it work? I don't really understand the concept of a loop without the braces and with the braces. I guess an explanation with a piece of code would help. This is in C by the way. Here's a code I've been looking at as a reference. int main(int argc, char* argv[]) { int i; int count = 0;

How can I loop through a List<T> and grab each item?

主宰稳场 提交于 2019-12-28 02:29:59
问题 How can I loop through a List and grab each item? I want the output to look like this: Console.WriteLine("amount is {0}, and type is {1}", myMoney.amount, myMoney.type); Here is my code: static void Main(string[] args) { List<Money> myMoney = new List<Money> { new Money{amount = 10, type = "US"}, new Money{amount = 20, type = "US"} }; } class Money { public int amount { get; set; } public string type { get; set; } } 回答1: foreach : foreach (var money in myMoney) { Console.WriteLine("Amount is

Rename the property names and change the values of multiple objects

倖福魔咒の 提交于 2019-12-28 02:03:48
问题 In the object below, I'd like to change the property name, thumb , to thumbnail . I'd also like to change the values of the title to include <span> tags. Here's my object: var data = [{ thumb: '/images/01.png', title: 'My title', },{ thumb: '/images/02.png', title: 'My title', },{ thumb: '/images/03.png', title: 'My title', }]; here's how I'd like it to look: var data = [{ thumbnail: '/images/01.png', title: '<span class="red">title 1</span>', },{ thumbnail: '/images/02.png', title: '<span

Technical reasons behind formatting when incrementing by 1 in a 'for' loop?

混江龙づ霸主 提交于 2019-12-28 01:57:27
问题 All over the web, code samples have for loops which look like this: for(int i = 0; i < 5; i++) while I used the following format: for(int i = 0; i != 5; ++i) I do this because I believe it to be more efficient, but does this really matter in most cases? 回答1: Everybody loves their micro-optimizations, but this would not make a difference as far as I can see. I compiled the two variations with g++ on for Intel processors without any fancy optimizations and the results are for for(int i = 0; i <