for-loop

Series Summation using for loop in python

感情迁移 提交于 2020-01-24 00:54:05
问题 Let's assume this series 1+2+3+....+n in c with for loop we can easily done this for(i=1;i<=n;i++) { sum += i; } in python i am able to done this using while loop while(num <= n): sum += num num = num+1 but i can't do it using python for loop 回答1: Python syntax is a bit different than c. In particular, we usually use the range function to create the values for the iterator variable (this is what was in Stephen Rauch's comment). The first argument to range is the starting value, the second is

How to call top level from lapply loop (skip/pass)

半世苍凉 提交于 2020-01-23 17:53:26
问题 I want to create a lapply loop that if attends a certain requirement, it will stop... as an example: score <- 0 lapply(1:100, function(z){ score <<- score + 1 if(score >=10){ break } }) However, there is no stop argument as break/pass in a lapply loop. I know this example sounds stupid. However, the original code has too many dependencies to be easily understood... My original loop removes an item from a vector an object every time, however, if there is nothing else to be removed from it can

jQuery callback after append

大憨熊 提交于 2020-01-23 17:34:07
问题 I have the following code: HTML <div id="body"></div> JS var site = { 'pageData' : [ { 'loadInTo' : '#aboutUs', 'url' : 'aboutUs.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#whatWeDo', 'url' : 'whatWeDo.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#ourValues', 'url' : 'ourValues.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#ourExpertise', 'url' : 'ourExpertise.html', 'urlSection' : '.sectionInner' } ]} for(i=0; i < site.pageData.length; i++) { var loader =

jQuery callback after append

雨燕双飞 提交于 2020-01-23 17:34:06
问题 I have the following code: HTML <div id="body"></div> JS var site = { 'pageData' : [ { 'loadInTo' : '#aboutUs', 'url' : 'aboutUs.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#whatWeDo', 'url' : 'whatWeDo.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#ourValues', 'url' : 'ourValues.html', 'urlSection' : '.sectionInner' }, { 'loadInTo' : '#ourExpertise', 'url' : 'ourExpertise.html', 'urlSection' : '.sectionInner' } ]} for(i=0; i < site.pageData.length; i++) { var loader =

Read multiple xml files in R and combine the data

妖精的绣舞 提交于 2020-01-23 15:57:10
问题 I have a folder containing more than 1000 files with the extension (they are no real xml files though). I want to extract certain contents from these files automatically, so that a matrix or table is the end result (which I can use further in R for analysis, or export to 1 csv file, etc). I have made/altered a code which works for a single file, but can't get it to work to do it automatically for the rest. By a loop? So my code for a single file is as follows: library(xml2) temp <- read_xml(

Is it possible to use for loop inside a switch?

前提是你 提交于 2020-01-23 09:44:11
问题 I'm attempting something like the following: switch ($p) { foreach ($modules as $m) { case '"'.$mod.'"': include 'modules/'.$m.'/cases.php'; break; } } but can't get it to work. Is it possible to use a for loop in this manner, inside a switch? 回答1: I don't think it is... Basic and shorter solution: foreach($modules AS $m) { if($p == $m) { include 'modules/'.$m.'/cases.php'; break; } } but the best would be: if(in_array($p, $modules)) include 'modules/'.$p.'/cases.php'; 回答2: Yes and No You

Instantiate a matrix with x zeros and the rest ones

家住魔仙堡 提交于 2020-01-23 08:20:41
问题 I would like to be able to quickly instantiate a matrix where the first few (variable number of) cells in a row are 0, and the rest are ones. Imagine we want a 3x4 matrix. I have instantiated the matrix first as all ones: ones = np.ones([4,3]) Then imagine we have an array that announces how many leading zeros there are: arr = np.array([2,1,3,0]) # first row has 2 zeroes, second row 1 zero, etc Required result: array([[0, 0, 1], [0, 1, 1], [0, 0, 0], [1, 1, 1]]) Obviously this can be done in

How to create tuple with a loop in python

霸气de小男生 提交于 2020-01-23 04:54:09
问题 I want to create this tuple: a=(1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5),(6,6,6),(7,7,7),(8,8,8),(9,9,9) I tried with this a=1,1,1 for i in range (2,10): a=a,(i,i,i) However it creates a tuple inside other tuple in each iteration. Thank you 回答1: Use an extra comma in your tuples, and just join: a = ((1,1,1),) for i in range(2,10): a = a + ((i,i,i),) Edit : Adapting juanpa.arrivillaga's comment, if you want to stick with a loop, this is the right solution: a = [(1,1,1)] for i in range (2,10): a

How to calculate and plot multiple linear trends for a time series?

牧云@^-^@ 提交于 2020-01-23 03:52:06
问题 Fitting a linear trend to a set of data is straight forward. But how can I fit multiple trend lines to one time series? I define up and down trends as prices above or below a exponential moving average. When the price is above the EMA I need to fit a positive trend and when the trend turns negative a new negative trend line and so forth. In my code below the market_data['Signal'] in my pandas dataframe tells me if the trend is up +1 or down -1. I'm guessing I need some kind of a loop, but I

Why doesn't a foreach loop work in certain cases?

本小妞迷上赌 提交于 2020-01-22 15:23:47
问题 I was using a foreach loop to go through a list of data to process (removing said data once processed--this was inside a lock). This method caused an ArgumentException now and then. Catching it would have been expensive so I tried tracking down the issue but I couldn't figure it out. I have since switched to a for loop and the problem seems to have went away. Can someone explain what happened? Even with the exception message I don't quite understand what took place behind the scenes. Why is