if-statement

Is using If, ElseIf, ElseIf better than using If, If, If?

好久不见. 提交于 2020-01-03 16:55:59
问题 Is there really any difference between using If(this) { } Else If(that) { } Else { } or using, If(this) { } If(that) { } Else { } ? Does one execute any faster? Does the compiler or architecture make any difference? 回答1: There's the huge difference that the contents of the this -block and the that -block can both be executed in the second form, whereas the first form allows at most one of those to be executed. Compare these two Python snippets: x = 10 if x > 5: print "x larger than 5" elif x

Shorthand expression for an if ( $a == $b || $a == $c ) statement

不问归期 提交于 2020-01-03 15:37:14
问题 I know this code will work: echo ( $a == $b || $a == $c ) ? "Yes" : "No"; That can be read like: if $a is equal to $b or $a is equal to $c Is there a way to make it more shorter like: if $a is equal to $b or $c I have tried a lot including this but still no luck: echo ( $a == ( $b xor $c ) ) ? "Yes" : "No"; 回答1: You can use in_array: var_dump(in_array($a, [$b, $c])); with your example: echo in_array($a, [$b, $c]) ? 'Yes' : 'No'; Note: this syntax is only useful if you have more than 2 values.

Can I use the “as” mechanism in an if statement

半世苍凉 提交于 2020-01-03 13:52:37
问题 Is it possible to use as in if statement like with that we use, for example: with open("/tmp/foo", "r") as ofile: # do_something_with_ofile This is my code: def my_list(rtrn_lst=True): if rtrn_lst: return [12, 14, 15] return [] if my_list(): print(my_list()[2] * mylist()[0] / mylist()[1]) Can I use if in this type: if my_list() as lst: print(lst[2] * lst[0] / lst[1]) In first if I called my_list four times. I can use a variable but I want to know is there any way to use as ? 回答1: No. The if

Can I use the “as” mechanism in an if statement

假装没事ソ 提交于 2020-01-03 13:52:30
问题 Is it possible to use as in if statement like with that we use, for example: with open("/tmp/foo", "r") as ofile: # do_something_with_ofile This is my code: def my_list(rtrn_lst=True): if rtrn_lst: return [12, 14, 15] return [] if my_list(): print(my_list()[2] * mylist()[0] / mylist()[1]) Can I use if in this type: if my_list() as lst: print(lst[2] * lst[0] / lst[1]) In first if I called my_list four times. I can use a variable but I want to know is there any way to use as ? 回答1: No. The if

Loop to add new columns with ifelse

☆樱花仙子☆ 提交于 2020-01-03 09:01:47
问题 I would like to make my code more efficient, I have a survey where my data looks like: survey <- data.frame( x = c(1, 6, 2, 60, 75, 40, 27, 10), y = c(100, 340, 670, 700, 450, 200, 136, 145)) #Two lists: A <- c(3, 6, 7, 27, 40, 41) t <- c(0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16) What I did was create new columns, like this: z <- ifelse(survey$x %in% A), 0, min(t)) for (i in t) { survey[paste0("T",i)] <-z survey[paste0("T",i)] <-ifelse (z > 0, i, z) } But with that code it takes a while, is

Changing parameters within function for use with ODE solver

余生颓废 提交于 2020-01-03 05:47:10
问题 Is is it possible to use an ODE solver, such as ode45, and still be able to 'change' values for the parameters within the called function? For example, if I were to use the following function: function y = thisode(t, Ic) % example derivative function % parameters a = .05; b = .005; c = .0005; d = .00005; % state variables R = Ic(1); T = Ic(2); y = [(R*a)-(T/b)+d; (d*R)-(c*T)]; with this script: clear all % Initial conditions It = [0.06 0.010]; % time steps time = 0:.5:10; % ODE solver [t,Ic1]

Efficiently search for first character of a string in a pandas dataframe

淺唱寂寞╮ 提交于 2020-01-03 05:39:07
问题 I have a pandas data frame column and I need to modify any entry of that column that starts with a 2. Right now, I'm using this which works, but is very, very slow: for i, row in df.iterrows(): if df['IDnumber'][i].startswith('2') == True: '''Do some stuff''' I feel (read: know) there's a more efficent way to do this without using a for loop but I can't seem to find it. Other things I've tried: if df[df['IDnumber'].str[0]] == '2': '''Do some stuff''' if df[df['IDnumber'].str.startswith('2')]

MYSQL hide field data if value in another field is set

霸气de小男生 提交于 2020-01-03 05:10:25
问题 Got a table like: art. type price a b 1 a c 2 would it be possible with a select to show filtered content as: art. type price a b a c 2 so that if the type is "b" don't show price data? select art, type, price from x where type="b" hide price 回答1: Logic of this sort probably best belongs in the presentation, rather than database, layer of your application. However, it is nevertheless possible using either MySQL's IF() function or its CASE expression—for example: SELECT art, type, IF(type='b'

On this event go to this url , else go here

左心房为你撑大大i 提交于 2020-01-03 04:53:11
问题 I am trying to write an if statment in php but I am not sure how to redirect the user to another url on a condition. What I am trying to do is : if $member = $notmember go here index.php?option=com_jumi&view=application&fileid=6&Itemid=174 else go here index.php?option=com_jumi&view=application&fileid=6&Itemid=124 I am not sure how to redirect to another url though ?. 回答1: You could use header . Something like this: if ($member == $notmember) header("Location: index.php?option=com_jumi&view

<= is not a prefix unary operator

别说谁变了你拦得住时间么 提交于 2020-01-03 03:25:08
问题 My task is to create an iOS application that will allow the user to enter the following information: • First Name • Last Name • Age The application will then display this information in a separate box as follows: Hi firstName lastName, you are Message. The Message will be replaced based on the age entered by the user as follows: If age is < 12 Message => “a child” If age is >= 13 and <= 19: Message => “a teenager” If age is > 19 and <=29: Message => “a young man” If age is >= 30 and <=49: