for-loop

Enclosing variables within for loop

半世苍凉 提交于 2019-12-29 07:06:09
问题 So consider the following chunk of code which does not work as most people might expect it to #cartoon example a <- c(3,7,11) f <- list() #manual initialization f[[1]]<-function(x) a[1]+x f[[2]]<-function(x) a[2]+x f[[3]]<-function(x) a[3]+x #desired result for the rest of the examples f[[1]](1) # [1] 4 f[[3]](1) # [1] 12 #attempted automation for(i in 1:3) { f[[i]] <- function(x) a[i]+x } f[[1]](1) # [1] 12 f[[3]](1) # [1] 12 Note that we get 12 both times after we attempt to "automate". The

Nested loop in Django template

橙三吉。 提交于 2019-12-29 07:01:11
问题 I can't get my head around this. I need to somehow access the object in the parent loop but I'm not sure how. Here is what I've come up with so far. I marked the problematic area in the code with XXX : Template: {% for item in ingrcat %} <h2>{{ item.name }}</h2> <ul> {% for ingr in XXX %} <li><a href="#" id="i{{ ingr.id }}">{{ ingr.name }}</a></li> {% endfor %} </ul> {% endfor %} XXX - should be a list of ingredients belonging to the ingredience category which is currently being looped

Java “for” statement implementation prevents garbage collecting

北城余情 提交于 2019-12-29 05:46:23
问题 UPD 21.11.2017: the bug is fixed in JDK, see comment from Vicente Romero Summary: If for statement is used for any Iterable implementation the collection will remain in the heap memory till the end of current scope (method, statement body) and won't be garbage collected even if you don't have any other references to the collection and the application needs to allocate a new memory. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8175883 https://bugs.openjdk.java.net/browse/JDK-8175883

python: order a list of numbers without built-in sort, min, max function

北战南征 提交于 2019-12-29 04:54:28
问题 If I have a list that varies in length each time and I want to sort it from lowest to highest, how would I do that? If I have: [-5, -23, 5, 0, 23, -6, 23, 67] I want: [-23, -6, -5, 0, 5, 23, 23, 67] I start with this: data_list = [-5, -23, 5, 0, 23, -6, 23, 67] new_list = [] minimum = data_list[0] # arbitrary number in list for x in data_list: if x < minimum: minimum = value new_list.append(i) BUT this only goes through once and I get: new_list = [-23] This is where I get stuck. How do I keep

How do I create multiple checkboxes from a list in a for loop in python tkinter

强颜欢笑 提交于 2019-12-29 04:49:10
问题 I have a list of variable length and want to create a checkbox (with python TKinter) for each entry in the list (each entry corresponds to a machine which should be turned on or off with the checkbox -> change the value in the dictionary). print enable {'ID1050': 0, 'ID1106': 0, 'ID1104': 0, 'ID1102': 0} (example, can be any length) now the relevant code: for machine in enable: l = Checkbutton(self.root, text=machine, variable=enable[machine]) l.pack() self.root.mainloop() This code produces

R: Text progress bar in for loop

落爺英雄遲暮 提交于 2019-12-29 04:18:04
问题 I have some sample code which contains a for loop and creates some plots like this (my actual data creates several thousands of plots): xy <- structure(list(NAME = structure(c(2L, 3L, 1L, 1L), .Label = c("CISCO","JOHN", "STEPH"), class = "factor"), ID = c(41L, 49L, 87L, 87L), X_START_YEAR = c(1965L, 1948L, 1959L, 2003L), Y_START_VALUE = c(940L,-1760L, 110L, 866L), X_END_YEAR = c(2005L, 2000L, 2000L, 2007L), Y_END_VALUE = c(940L, -1760L, 110L, 866L), LC = structure(c(1L,1L, 2L, 2L), .Label = c

using anonymous function in javascript for loops

…衆ロ難τιáo~ 提交于 2019-12-29 03:21:08
问题 I have seen anonymous functions inside for loops to induce new scope on the web in one or two places and would like to know if it makes sense. for example: var attr, colors = ['green','blue','red']; for ( attr = 0; attr < colors.length; attr++) { (function() { var colorAttr = colors[attr]; // do something with colorAttr })(); } I understand it has something to do with keeping the scope inside the for loop clean, but in what situations would this be necessary? Would it be good practice to do

Why are arbitrary target expressions allowed in for-loops?

我的梦境 提交于 2019-12-29 02:47:10
问题 I accidentally wrote some code like this: foo = [42] k = {'c': 'd'} for k['z'] in foo: # Huh?? print k But to my surprise, this was not a syntax error. Instead, it prints {'c': 'd', 'z': 42} . My guess is that the code is translated literally to something like: i = iter(foo) while True: try: k['z'] = i.next() # literally translated to assignment; modifies k! print k except StopIteration: break But... why is this allowed by the language? I would expect only single identifiers and tuples of

How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average?

北城余情 提交于 2019-12-29 02:01:09
问题 I have been at this for hours!!!! An update to the assignment states that we need to stop the user input when their values exceed 100. Without rewriting the whole thing, how do I "loop" this in? I know the code is method heavy but it was a requirement for the assignment. I think my brain is just java-soup! Any help would be AWESOME :) public static void main(String[] args) { //Use Main Method for gathering input float input = 1; // Declare variable for sum float theSum = 0; // Declare

Scala: strange behavior in `for` pattern matching for None case

久未见 提交于 2019-12-29 01:37:08
问题 Strange behavior in for cycle pattern matching: scala> val a = Seq(Some(1), None) a: Seq[Option[Int]] = List(Some(1), None) scala> for (Some(x) <- a) { println(x) } 1 scala> for (None <- a) { println("none") } none none Why in second example two output 'none' produced? Maybe this example is synthetic and not practical, but such behavior is not expectable. Is this bug or feature? 回答1: What do you know, it is a bug: https://issues.scala-lang.org/browse/SI-9324 scala> val vs = Seq(Some(1), None)