loops

What's the higher-performance alternative to for-loops for subsetting data by group-id?

我的未来我决定 提交于 2020-01-22 14:38:07
问题 A recurring analysis paradigm I encounter in my research is the need to subset based on all different group id values, performing statistical analysis on each group in turn, and putting the results in an output matrix for further processing/summarizing. How I typically do this in R is something like the following: data.mat <- read.csv("...") groupids <- unique(data.mat$ID) #Assume there are then 100 unique groups results <- matrix(rep("NA",300),ncol=3,nrow=100) for(i in 1:100) { tempmat <-

C++ - If an object is declared in a loop, is its destructor called at the end of the loop?

[亡魂溺海] 提交于 2020-01-22 13:44:45
问题 In C++, an object's destructor is called at the closing "}" for the block it was created in, right? So this means that if I have: while(some_condition) { SomeClass some_object; some_object.someFunction(); some_variable = some_object.some_member; } Then the destructor for the object created in one iteration of the loop will be called at the end of the loop before another object is created, correct? Thanks. 回答1: Yes. But you could have tested it yourself. This is a language feature that

closure in for-loop > different tries failed

烈酒焚心 提交于 2020-01-22 03:42:09
问题 I want to create a 2-dimensional array with an index-number in each first element. EDIT: thx a lot so far.. @carl: I did so much function creation just to show the kind of tries I did.. jonhopkins idea gave rise to this: this works: $('#create_indexed_array').click(function() { var new_array = [[9,9],[9,9],[9,9],[9,9],[9,9]]; for (var i = 0; i < 5; i++) { new_array[i][0] = i; } alert(JSON.stringify(new_array)); }); BUT this works not: $('#create_indexed_array').click(function() { var new

How do I properly loop through the subplots of a matplotlib figure to show the final y-axis value of each line?

浪尽此生 提交于 2020-01-22 03:32:04
问题 I currently have a matplotlib figure with 16 subplots, 4 columns and 4 rows. I have the following code produced that loops through each subplot and plots different data on each of the 16 subplots: fac_list = [one, two, three, four, five, six, seven, eight, nine, ten , eleven, twelve, thirteen, fourteen, fifteen, sixteen] color = ['blue','red'] ds_i = 0 for row in range(0,subplot_shape[0]): for col in range(0,subplot_shape[1]): fac = fac_list[ds_i] plot('total', currentname=fac, subplot_index=

Create Login and Logout Loop Python Selenium

為{幸葍}努か 提交于 2020-01-22 02:14:07
问题 Hi I'm trying to Create a login and logout loop that I've had to www.shopee.com.my from selenium import webdriver import time from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys import csv import urllib.parse url = 'https://shopee.com.my/' driver = webdriver.Chrome('E:

Stuck with loops in python - only returning first value

丶灬走出姿态 提交于 2020-01-22 02:04:40
问题 I am a beginner in Python trying to make a function that will capitalize all of the values with an even index, and make lowercase all of the values with an odd index. I have been struggling repeatedly with for loops only giving me the first value. I have also tried with while loops. However I am curious if there is a way to make it work with for loops (do I need a '+=1' somewhere?) def func1(x): for (a,b) in enumerate (x): if a%2 == 0: return b.upper() else: return b.lower() func1('Testing

comparison of loop and vectorization in matlab

℡╲_俬逩灬. 提交于 2020-01-22 00:20:41
问题 let us consider following code for impulse function function y=impulse_function(n); y=0; if n==0 y=1; end end this code >> n=-2:2; >> i=1:length(n); >> f(i)=impulse_function(n(i)); >> returns result f f = 0 0 0 0 0 while this code >> n=-2:2; >> for i=1:length(n); f(i)=impulse_function(n(i)); end >> f f = 0 0 1 0 0 in both case i is 1 2 3 4 5,what is different? 回答1: Your function is not defined to handle vector input. Modify your impluse function as follows: function y=impulse_function(n) [a b

PHP Iterating simplexml in foreach loop

北战南征 提交于 2020-01-21 22:11:23
问题 I have a simplexml object which looks as below <?xml version="1.0"?> <SalesInvoices xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://api.unleashedsoftware.com/version/1"> <SalesInvoice> <OrderNumber>100</OrderNumber> </SalesInvoice> <SalesInvoice> <OrderNumber>101</OrderNumber> </SalesInvoice> </SalesInvoices> I want to iterate through it and print only the order number. I use this script: foreach ($xml->SalesInvoices-

Python looping through string and matching it with with wildcard pattern

醉酒当歌 提交于 2020-01-21 19:24:12
问题 string1="abc" string2="abdabcdfg" I want to find if string1 is substring of string2. However, there are wildcard characters like "." can be any letter, y can be "a" or "d" , x can be "b" or "c" . as a result, ".yx" will be substring of string2 . How can I code it using only one loop? I want to loop through string2 and make comparisons at each index. i tried dictionary but I wand to use loop my code: def wildcard(string,substring): sum="" table={'A': '.', 'C': '.', 'G': '.', 'T': '.','A': 'x',

Python looping through string and matching it with with wildcard pattern

最后都变了- 提交于 2020-01-21 19:24:07
问题 string1="abc" string2="abdabcdfg" I want to find if string1 is substring of string2. However, there are wildcard characters like "." can be any letter, y can be "a" or "d" , x can be "b" or "c" . as a result, ".yx" will be substring of string2 . How can I code it using only one loop? I want to loop through string2 and make comparisons at each index. i tried dictionary but I wand to use loop my code: def wildcard(string,substring): sum="" table={'A': '.', 'C': '.', 'G': '.', 'T': '.','A': 'x',