首页 \ 问答 \ freemarker怎么遍历map集合

freemarker怎么遍历map集合

后台:Map> map = new HashMap>(); html: <#if map?exists>                 <#list map?keys as key>                            ${key}                             现在我想获取value中的值,html中该怎么写
更新时间:2022-03-05 22:03

最满意答案

我对你的代码做了一些小调整而没有改变它的整体风格。 注意Yoong Kim的建议并尝试将代码分解成更小的部分,以使其更具可读性和可维护性,这将是一件好事。

  • 您的函数现在获得两个“n”参数,表示每行中有多少个样本,以及您需要多少次迭代(列)。

  • 你在循环中增加了数组MedianMean ,这需要很多关于重新分配内存和复制内容的麻烦,这会减慢一切。 我已经预定义X_after并在循环后移动平均值和中值计算以避免这种情况。 (作为奖励, meanmedian仅被调用一次而不是n_iteration次。)

  • ifelse的调用并不是真的需要。

  • 调用rlnorm一次,为x和lod生成足够的值比调用它两次要快一点。

这是更新的功能。

dset2 <- function (mu, sigma, n_samples, n_iterations, p) {    
  X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
  pct_cens <- numeric(n_iterations)
  count <- 1
  while(count <= n_iterations) {     
    random_values <- rlnorm(2L * n_samples, log(mu), log(sigma))
    lod <- quantile(random_values[1:n_samples], p = p)
    X_before <- random_values[(n_samples + 1L):(2L * n_samples)]
    X_after[count, ] <- pmax(X_before, lod)
    delta <- X_before <= lod
    pct_cens[count] <- mean(delta)
    if (pct_cens > 0 && pct_cens < 1 ) count <- count + 1
  }

  Median <- apply(X_after, 1, median)
  Mean <- rowMeans(X_after)
  data.frame(Pct_cens_array=pct_cens, Mean=Mean, Median=Median) 
}

比较时间,例如,

mu=1
sigma=3
n_samples=10L
n_iterations = 1000L
p=0.10
system.time(dset(mu,sigma, n_samples, n_iterations, p))
system.time(dset2(mu,sigma, n_samples, n_iterations, p))

在我的机器上,有3倍加速。


I've made a few little tweaks to your code without changing the whole style of it. It would be good to heed Yoong Kim's advice and try to break up the code into smaller pieces, to make it more readable and maintainable.

  • Your function now gets two "n" arguments, for how many samples you have in each row, and how many iterations (columns) you want.

  • You were growing the arrays Median and Mean in the loop, which requires a lot of messing about reallocating memory and copying things, which slows everything down. I've predefined X_after and moved the mean and median calculations after the loop to avoid this. (As a bonus, mean and median only get called once instead of n_iteration times.)

  • The calls to ifelse weren't really needed.

  • It is a little quicker to call rlnorm once, generating enough values for x and the lod, than to call it twice.

Here's the updated function.

dset2 <- function (mu, sigma, n_samples, n_iterations, p) {    
  X_after <- matrix(NA_real_, nrow = n_iterations, ncol = n_samples)
  pct_cens <- numeric(n_iterations)
  count <- 1
  while(count <= n_iterations) {     
    random_values <- rlnorm(2L * n_samples, log(mu), log(sigma))
    lod <- quantile(random_values[1:n_samples], p = p)
    X_before <- random_values[(n_samples + 1L):(2L * n_samples)]
    X_after[count, ] <- pmax(X_before, lod)
    delta <- X_before <= lod
    pct_cens[count] <- mean(delta)
    if (pct_cens > 0 && pct_cens < 1 ) count <- count + 1
  }

  Median <- apply(X_after, 1, median)
  Mean <- rowMeans(X_after)
  data.frame(Pct_cens_array=pct_cens, Mean=Mean, Median=Median) 
}

Compare timings with, for example,

mu=1
sigma=3
n_samples=10L
n_iterations = 1000L
p=0.10
system.time(dset(mu,sigma, n_samples, n_iterations, p))
system.time(dset2(mu,sigma, n_samples, n_iterations, p))

On my machine, there is a factor of 3 speedup.

相关问答

更多
  • 循环遍历行是不必要的,你可以用ifelse矢量ifelse循环: x[1:3] <- lapply(1:3, function(n) ifelse(x[[n+3]] == 0, NA, x[[n]])) x # c1 c2 c3 c4 c5 c6 #r1 1 4 7 4 3 2 #r2 NA 5 8 0 8 8 #r3 3 NA 9 9 0 5 #r4 2 2 NA 1 7 0 #r5 1 3 1 5 3 5 #r6 NA 4 6 0 6 ...
  • 我对你的代码做了一些小调整而没有改变它的整体风格。 注意Yoong Kim的建议并尝试将代码分解成更小的部分,以使其更具可读性和可维护性,这将是一件好事。 您的函数现在获得两个“n”参数,表示每行中有多少个样本,以及您需要多少次迭代(列)。 你在循环中增加了数组Median和Mean ,这需要很多关于重新分配内存和复制内容的麻烦,这会减慢一切。 我已经预定义X_after并在循环后移动平均值和中值计算以避免这种情况。 (作为奖励, mean和median仅被调用一次而不是n_iteration次。) 对if ...
  • 虽然您使用的是Devectorize.jl包,但我建议您将所有这些矢量化操作明确地写成简单的循环。 我预计这会给你带来显着的性能提升。 Devectorize包当然是一个伟大的贡献,但为了看到跳过它来为你做肮脏的工作,你可以做这样的事情(来自包README的一个例子): using Devectorize a = rand(2,2); b = rand(2,2); c = rand(2,2); julia> macroexpand(:(@devec r = exp(a + b) .* sum(c))) ...
  • 这是一个通用(可扩展)的方法,速度也非常快: sets <- list(A, B, C, D, E, G) vals <- c(1, 2, 3, 4, 5, 7) keys <- unlist(sets) values <- rep(vals, sapply(sets, length)) df$foo <- values[match(df$foo, keys)] Here is a general (scalable) approach, very fast too: sets <- list ...
  • 如果一切都失败了,你可以结合使用这两种方法: 再次猜测环境:问问你自己如何实现它的功能,然后推断哪些功能可能需要最少的计算工作。 试错法:只是通过测试它们来比较不同的方法。 如果环境有一定的代码定时功能,例如(准确地)告诉你现在是什么时间的函数,那么这会有很大的帮助。 不要忘记内存缓存和优化等效果。 如果您尝试在特定环境中使用特定功能,它可能会与您以前的体验有所不同。 If all else fails, you can use a combination of these two approaches: ...
  • 如果您只想在数字0,1,...,9上获得离散的均匀分布,那么只需使用样本 sample(0:9, k, replace = TRUE) 使用你现在拥有的代码,你实际得到的概率为0.05,每个得到0或10,概率为.10,得到1-9。 If all you want to do is get a discrete uniform distribution on the numbers 0, 1, ..., 9 then just use sample sample(0:9, k, replace = TRUE ...
  • 我认为dplyr更整洁。 正确使用recode是个好主意。 mutate_all()可用于对整个数据帧进行操作, mutate_at()仅对所选变量进行操作。 有很多方法可以在dplyr中指定变量。 mydata <- data.frame(arg1=c(1,2,4,5),arg2=c(1,1,2,0),arg3=c(1,1,1,1)) mydata mydata <- mydata %>% mutate_at(c("arg1","arg2"), funs(recode(., `1`=-1, ...
  • 在许多方面,可以改进此代码并使其更快。 看起来你基本上是在编写C风格的代码,而不是利用内置的矢量化R函数。 这是一个例子。 这部分代码: DD <- vector(length = R) ConceptIDSource <- MITMatrix[Source,q] counterq <- 1 # counterq is a pointer to cell of vector DD that keep the co ...
  • 在使用嵌套循环时,检查outer()是否不适合你的工作总是很有趣。 outer()是嵌套循环的向量化解决方案; 它将向量化函数应用于前两个参数中元素的每个可能组合。 由于stringdist()对向量起作用,因此您可以简单地执行: library(stringdist) strings <- c("Hello", "Helo", "Hole", "Apple", "Ape", "New", "Old", "System", "Systemic") outer(strings,s ...
  • SIN_FM5 <- data.frame(Combination = sample(1:10, 100, repl=TRUE), MONTANT_PAIEMENT=rnorm(100)) bySIN <- by(SIN_FM5, list(SIN_FM5[['Combination']]), FUN= function(subd) { data.frame(counts = nrow(subd), meanMont = mean(subd$MONT ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。