for-loop

Why is in my case For loop faster vs Map, Reduce and List comprehension

北城以北 提交于 2020-12-31 04:48:47
问题 I wrote a simple script that test the speed and this is what I found out. Actually for loop was fastest in my case. That really suprised me, check out bellow (was calculating sum of squares). Is that because it holds list in memory or is that intended? Can anyone explain this. from functools import reduce import datetime def time_it(func, numbers, *args): start_t = datetime.datetime.now() for i in range(numbers): func(args[0]) print (datetime.datetime.now()-start_t) def square_sum1(numbers):

Looping over unique pairs of elements in a list in R

大憨熊 提交于 2020-12-30 04:44:31
问题 Suppose I have a list of objects called bb . I want to pick each unique pair of elements in bb and use them in some kind of function (called convolve ) as shown below: ## Below `bb` elements: `bma1` & `bma2` are used in the function: con <- convolve(dens1= bb$bma1$dposterior, dens2= function(x){bb$bma2$dposterior(-x)}, cdf1= bb$bma1$pposterior, cdf2= function(x){1 - bb$bma2$pposterior(-x)}) con$quantile(c(0.025, 0.975)) Question: bb can have any number of elements but convolve() accepts only

How to optimize a function that matches observations according to certain criteria

狂风中的少年 提交于 2020-12-30 04:20:30
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

泪湿孤枕 提交于 2020-12-30 04:09:55
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

帅比萌擦擦* 提交于 2020-12-30 04:09:41
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

How to optimize a function that matches observations according to certain criteria

痞子三分冷 提交于 2020-12-30 04:08:31
问题 I am looking for a more efficient way of doing an operation with a given dataframe. library(purrr) library(dplyr) Here is a step by step description: First, there is the function possible_matches , that for each observation i in df , gives the index of rows that are possibly matchable to i , which are going to be used on the next step: possible_matches <- function(i, df) { k1 <- df$j[df$id_0 == df$id_0[i]] j2 <- setdiff(df$j, k1) k2 <- map(j2, ~ df$j[df$id_0[.] == df$id_0]) k3 <- map(k2, ~

Using sudo with for loop

左心房为你撑大大i 提交于 2020-12-29 05:17:27
问题 I want to run a simple for loop command with sudo, but it isn't working: sudo -i -u user for i in /dir; do echo $i; done I get the following error: -bash: syntax error near unexpected token `do' Probably a very simple thing I am overlooking. Any help? 回答1: sudo wants a program (+arguments) as a parameter, not a piece of shell script. You can do this, though: sudo -i -u user sh -c 'for i in /dir; do echo $i; done' Note the single quotes. If you used double quotes, your shell would try to

Using forloop.counter value as list index in a Django template

こ雲淡風輕ζ 提交于 2020-12-29 05:16:08
问题 in my Django 1.1.1 application I've got a function in the view that returns to his template a range of numbers and a list of lists of items, for example: ... data=[[item1 , item2, item3], [item4, item5, item6], [item7, item8, item9]] return render_to_response('page.html', {'data':data, 'cycle':range(0,len(data)-1]) Inside the template I've got an external for loop, that contains also another for cycle to display in output the contains of the inner lists of data in this way ... {% for page in

Using forloop.counter value as list index in a Django template

自作多情 提交于 2020-12-29 05:14:28
问题 in my Django 1.1.1 application I've got a function in the view that returns to his template a range of numbers and a list of lists of items, for example: ... data=[[item1 , item2, item3], [item4, item5, item6], [item7, item8, item9]] return render_to_response('page.html', {'data':data, 'cycle':range(0,len(data)-1]) Inside the template I've got an external for loop, that contains also another for cycle to display in output the contains of the inner lists of data in this way ... {% for page in

Using forloop.counter value as list index in a Django template

五迷三道 提交于 2020-12-29 05:14:20
问题 in my Django 1.1.1 application I've got a function in the view that returns to his template a range of numbers and a list of lists of items, for example: ... data=[[item1 , item2, item3], [item4, item5, item6], [item7, item8, item9]] return render_to_response('page.html', {'data':data, 'cycle':range(0,len(data)-1]) Inside the template I've got an external for loop, that contains also another for cycle to display in output the contains of the inner lists of data in this way ... {% for page in