First, split the max_hf data set into two groups, Y and N.
def split_data_hf(old_data, new_data, variable, category):
new_data = old_data[old_data.variable == category]
split_data_hf(max_hf, max_hf1, inducted, 'Y')
split_data_hf(max_hf, max_hf2, inducted, 'N')
When I try to run this, I get the error that the variable inducted (which I am trying to pass through) is not defined. Can anyone explain why this is the case?
Theoretically it should work, and if I remove the variable input from the split_data_hf function and then add inducted in place of variable, then it runs just fine.
Anyways, I think I figured it out myself.
Instead of having
old_data[old_data.variable == category]
One should write:
old_data[old_data[variable] == category]
Then, when the input variable is passed, write "...." to pass the argument through.
Thanks!
Related
If I do something wrong with the formatting or anything else, sorry, I don't use this site often.
I'm trying to make a function in lua that with take a name I give it, and create a subtable with that name, and when I tried something like this it just made the absolute name I put in the function's code.
NewSubtable =
function(SubtableName)
Table.SubtableName = {} --Creates a subtable called SubtableName
end
How can I make this create a subtable that is called by the name I give in the function when I use it? Is there an indicator or something to let the code know not to use the name given, but to use the variable assigned when I use the function?
EDIT: So whenever I try this, I get the the result "table index is nil" and it points to the error on line 4
I went and tested this but with a different input type, and it was just my fault. I didn't think that strings would the the value type you'd need for what I'm doing. My problem is solved.
Complete code:
Items = {}
NewWeapon = function(id, name, desc, minDMG, maxDMG)
Items[id] = {}
Items[id].Name = name
Items[id].Desc = desc
Items[id].MinDMG = minDMG
Items[id].MaxDMG = maxDMG
end
NewWeapon(Test, "test", "test", 1, 1)
Table.SubtableName is actually a syntactic sugar for Table['SubtableName']. To use the contents of variable SubtableName use the idom Table[SubtableName].
I am making a terminal emulator in Python 3. The commands are being stored in functions, like:
def rd(os_vartmp, os_vartmp2):
if os_vartmp == None:
print('rd [path] [-S]')
print('Delete a folder')
else:
if os.path.isfile(os_vartmp) == True:
if os_vartmp2 == '-S': print('a')
else:
print(ERR5)
a = input('Command: ')
The terminal works like this:
Asks user for input
Splits the input
Uses the first part of input to search a function in locals
If there is one, uses the rest part of input as argument
Calls the function
The thing here is, when i call the function 'rd' with, for example, 'rd "boot.py" -S' it works just fine. But if i need to call it like this: rd "boot.py", it throws me a error about 1 argument given when 2 are required. Is there a fix for that?
You can make an argument optional by assigning a value in the method definition. For example:
def Add(x=0, y=0):
return x+y
If you input only one value, y will default to 0. If I wanted to give y a value but have x fall back on it's default value I could do Add(y=10). I hope this helped!
Have you tried this?
def rd(os_vartmp, os_vartmp2="-S"):
Instead of trying to get null value, which would require rd("boot.py",null), you can ser default value and then you can do rd("boot.py").
Hope it works.
I found it really useful to reuse a once created coroutine. I found a solution to that and it looks like so:
co = coroutine.create(function (f, args)
while f do
f = coroutine.yield(f(args))
end
end)
function dummyFunc(data)
print("XXX "..data)
coroutine.yield()
print("OOO "..data)
end
coroutine.resume(co, dummyFunc, "1")
coroutine.resume(co, dummyFunc, "2")
coroutine.resume(co, dummyFunc, "3")
coroutine.resume(co, dummyFunc, "4")
That work like a charm except the output is not:
XXX 1
OOO 2
XXX 3
OOO 4
It is:
XXX 1
OOO 1
XXX 1
OOO 1
So is it possible to change the arguments to the dummyFunc between the resume calls?
Think about this. The way coroutines work is like this. When you first resume them, the arguments you pass to resume become the arguments to the coroutine's function. When the coroutine yields, the arguments it passes to yield become the return values from your resume call.
However, the second time you resume the coroutine, it does not reach into the still executing function and change the arguments that were pass in the first time. It would be exceedinly rude to change the value of variables local to the function.
Therefore, the arguments to resume on calls after the first call will be the return values from yield.
co = coroutine.create(function (f, args)
while f do
f = coroutine.yield(f(args))
end
end)
So you'd need to do this:
co = coroutine.create(function (f, args)
while f do
f, args = coroutine.yield(f(args))
end
end)
However, if you want something more flexible, that can do variable numbers of arguments, you'll need to be cleverer:
co = coroutine.create(function (...)
local function capture_args(...)
return {...}, select("#", ...)
end
local tbl, len = capture_args(...)
local f = tbl[1]
while f do
tbl, len = capture_args(coroutine.yield(f(unpack(tbl, 2, len))
f = tbl[1]
end
end)
Some people wouldn't bother with the capture_args stuff, simply relying on {...} and calling unpack on it. This is safer because users can put nil values in parameter lists. ... will record all of the parameters, even embedded nils (but not trailing ones). However, once you put it into an array, the length of the array is based on the first nil value.
Using capture_args, you can get the actual parameter count thanks to a little-known-feature of select. And thanks to the ability of unpack to work on a given range, even if the range exceeds the length of the table, you can effectively store a parameter list.
I could probably make capture_args a bit cleverer by putting the length in the table it returns. But this is good enough for me.
There is a second problem here: you are yielding within dummyFunc, and dummyFunc does not seem to understand what to do with yield's return values (ie: the parameters to your next resume call).
It's not clear how you want dummyFunc to respond to it. If you wanted dummyFunc's parameters to change because of how you resumed it without dummyFunc knowing about it, that's not going to happen.
The use of the command "return" has always been bothering me since I started learning Python about a month ago(completely no programming background)
The function "double()" seems working fine without me have to reassign the value of the list used as an argument for the function and the value of the elements processed by the function would double as planned. Without the need to assign it outside the function.
However, the function "only_upper()" would require me to assign the list passed as argument through the function in order to see the effect of the function. I have to specify t=only_upper(t) outside of the function to see the effect.
So my question is this: Why are these two seemingly same function produces different result from the use of return?
Please explain in terms as plain as possible due to my inadequate programming skill. Thank you for your input.
def double(x):
for i in range(len(x)):
x[i] = int(x[i])*2
return x
x = [1, 2, 3]
print double(x)
def only_upper(t):
res = []
for s in t:
if s.isupper():
res.append(s)
t = res
return t
t = ['a', 'B', 'C']
t = only_upper(t)
print t
i am assuming that this is your first programming language hence the problem with understanding the return statement found in the functions.
The return in our functions is a means for us to literally return the values we want from that given 'formula' AKA function. For example,
def calculate(x,y):
multiply = x * y
return multiply
print calculate(5,5)
the function calculate defines the steps to be executed in a chunk. Then you ask yourself what values do you want to get from that chunk of steps. In my example, my function is to calculate the multiplied value from 2 values, hence returning the multiplied value. This can be shorten to the following
def calculate(x,y):
return x * y
print calculate(5,5)
not sure did anyone ever face this kind of problem. here is my code
in main.lua :
local highScore = require("highScore")
local username = "myName"
local finishedTime = 12345
highScore:InsertHighScore(userName, finishedTime)
in highScore.lua
function InsertHighScore(name,time)
print(name)
print(time)
-- other code
end
it look simple and shouldn't be wrong, but in my console out put it show :
table: 0x19e6340
myName
after a day of testing, i found that before the 2 parameter that i pass, it actually passing another table to me, so do these changes on highScore.lua:
function InsertHighScore(table,name,time)
print(table)
print(name)
print(time)
-- other code
end
so now my "other code" can work nicely, but why it pass me a table before my parameter ?
In Lua, a call to an object/table with a colon instead of a dot indicates that the object/table should be passed into the function as the first parameter (e.g, as a self). If you don't care about that, then call the function with a dot instead:
highScore.InsertHighScore(userName, finishedTime)