variables

Order data.table by a character vector of column names

浪尽此生 提交于 2020-07-08 05:27:07
问题 I'd like to order a data.table by a variable holding the name of a column: I've tried every combination of + eval , get and c` without success: I have colVar = "someColumnName" I'd like to apply this to: DT[order(colVar)] 回答1: You can use double brackets for data tables: library(data.table) dtbl <- data.table(x = 1:5, y = 5:1) colVar = "y" dtbl_sorted <- dtbl[order(dtbl[[colVar]])] dtbl_sorted 回答2: data.table has special functions for that matter which will modify your data set by reference

Javascript variable declaration within loop

蹲街弑〆低调 提交于 2020-07-06 11:12:29
问题 I have a habit that I am borderline compulsive about, but I think may be completely unnecessary. With code like: function abc(){ var a,b; for(var i=0;i<10;i++){ a=document.getElementsByTagName('LI').item(i).width; b=document.getElementsByTagName('DIV').item(i).width; // now do something with a and b } return; } I am compulsive about declaring the variable before the loop as opposed to: function abc(){ for(var i=0;i<10;i++){ var a=document.getElementsByTagName('LI').item(i).width; var b

Python - how to generate list of variables with new number at end

只愿长相守 提交于 2020-07-05 04:52:06
问题 What I would like to do is find a more concise way of creating variables of empty lists that will be similar to each other, with the exception of different numbers at the end. #For example: var1 = [] var2 = [] var3 = [] var4 = [] #... varN = [] #with the end goal of: var_list = [var1,var2,var3,var4, ... varN] 回答1: Use a list comprehension: [[] for n in range(N)] or a simple for loop empt_lists = [] for n in range(N): empt_lists.append([]) Note that [[]]*N does NOT work, it will use the same

How to share variable between functions in python?

…衆ロ難τιáo~ 提交于 2020-07-05 02:46:56
问题 I have 2 functions fun1 and fun2 which take as inputs a string and a number respectively. The also both get as input the same variable a . This is the code: a = ['A','X','R','N','L'] def fun1(string,vect): out = [] for letter in vect: out. append(string+letter) return out def fun2(number,vect): out = [] for letter in vect: out.append(str(number)+letter) return out x = fun1('Hello ',a) y = fun2(2,a) The functions perform some nonsense operations. My goal would be to rewrite the code in such a

DateTime Modify() php affecting previous variable [duplicate]

﹥>﹥吖頭↗ 提交于 2020-07-03 06:55:29
问题 This question already has answers here : How do I deep copy a DateTime object? (5 answers) Closed 2 years ago . So I'm having a weird issue with DateTimes modify() function. I start off with a DateTime eg: 2018-08-07 12:00 & a number of days to add eg: 2. I copy the dateTime (in variable $startDt) to a new variable ($date) so its not affected by any changes. The modify function works fine. I get 2018-08-09 12:00. But then I want to repeat the action with a new number but the same start date.

What does [:] mean in groovy?

霸气de小男生 提交于 2020-07-03 01:45:41
问题 While reading some groovy code of another developer I encountered the following definition: def foo=[:] What does it mean? 回答1: [:] is shorthand notation for creating a Map. You can also add keys and values to it: def foo = [bar: 'baz'] 回答2: [:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:] is roughly equivalent to this java code: Object foo = new java.util.LinkedHashMap(); 回答3: Quoting the doc: Notice that [:

What is the difference between constant variables and final variables in java?

笑着哭i 提交于 2020-07-02 08:37:59
问题 Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it. 回答1: Constant is the concept, the property of the variable. final is the java keyword to declare a constant variable. As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness. Quoting the specification, anyway, we can read A variable of primitive type [...],

What is the difference between constant variables and final variables in java?

橙三吉。 提交于 2020-07-02 08:36:31
问题 Please help me understand the difference between constant variables and final variables in Java. I am a bit confused with it. 回答1: Constant is the concept, the property of the variable. final is the java keyword to declare a constant variable. As other people pointed out, from a semantic/linguistic point of view the expression constant variable is an oxymoron and, as such, we could argue about its correctness. Quoting the specification, anyway, we can read A variable of primitive type [...],

how to get variable from another class python

只愿长相守 提交于 2020-06-29 09:59:15
问题 how can i get a variable from another class like that white=(255,255,255) class window: def open(a,b,c): displaysurf=pygame.display.set_mode(a) pygame.display.set_caption(b) displaysurf.fill(c) def close(): while True: for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() pygame.display.update() class img(): def show(image,position): pygame.image.load(image) displaysurf.blit(image,position) i want to get the displaysurf variable from window.open() function 回答1: You

Pyomo creating a variable time index

≡放荡痞女 提交于 2020-06-29 05:07:35
问题 I'm trying to bring this constraint in my pyomo model [ 1 I define a set for indexing over time and I want to optimize the corresponding energy variable below model.grid_time = Set(initialize=range(0, 23))) model.charging_energy = Var(model.grid_time, initialize=0) My constraint definition looks like as follows: model.limits = ConstraintList() for t in model.grid_time: model.limits.add(sum(model.charging_energy[t] for t in model.grid >= energy_demand.at[t,"total_energy_demand"]) The problem