conditional-statements

Why Switch/Case and not If/Else If?

孤街浪徒 提交于 2019-12-17 02:45:49
问题 This question in mainly pointed at C/C++, but I guess other languages are relevant as well. I can't understand why is switch/case still being used instead of if/else if. It seems to me much like using goto's, and results in the same sort of messy code, while the same results could be acheived with if/else if's in a much more organized manner. Still, I see these blocks around quite often. A common place to find them is near a message-loop (WndProc...), whereas these are among the places when

Replace all elements of Python NumPy Array that are greater than some value

自闭症网瘾萝莉.ら 提交于 2019-12-17 02:28:30
问题 I have a 2D NumPy array and would like to replace all values in it greater than or equal to a threshold T with 255.0. To my knowledge, the most fundamental way would be: shape = arr.shape result = np.zeros(shape) for x in range(0, shape[0]): for y in range(0, shape[1]): if arr[x, y] >= T: result[x, y] = 255 What is the most concise and pythonic way to do this? Is there a faster (possibly less concise and/or less pythonic) way to do this? This will be part of a window/level adjustment

Conditional Replace Pandas

☆樱花仙子☆ 提交于 2019-12-16 19:23:13
问题 I'm probably doing something very stupid, but I'm stumped. I have a dataframe, and I want to replace the values in a particular column that exceed a value with zero. I had thought this was a way of achieving this: df[df.my_channel > 20000].my_channel = 0 If I copy the channel into a new data frame it's simple: df2 = df.my_channel df2[df2 > 20000] = 0 this does exactly what I want, but seems not to work with the channel as part of the original dataframe. 回答1: .ix indexer works okay for pandas

Conditional Replace Pandas

戏子无情 提交于 2019-12-16 19:21:10
问题 I'm probably doing something very stupid, but I'm stumped. I have a dataframe, and I want to replace the values in a particular column that exceed a value with zero. I had thought this was a way of achieving this: df[df.my_channel > 20000].my_channel = 0 If I copy the channel into a new data frame it's simple: df2 = df.my_channel df2[df2 > 20000] = 0 this does exactly what I want, but seems not to work with the channel as part of the original dataframe. 回答1: .ix indexer works okay for pandas

Linq where clause with multiple conditions

吃可爱长大的小学妹 提交于 2019-12-14 00:50:15
问题 this method returns generic list but it has multiple condition to get select. I'm just writing this using if - else if -else if.... so many if else i mean Is there a shorter way to do this? Thank you. public List<ProductReqNoDate> GetRequestsQuery(string departmant, int reqStateID, string firstDate, string lastDate) { var db = new requestsDBEntities(); var listPrn = new List<ProductReqNoDate>(); if (!string.IsNullOrEmpty(departmant)) { return (from r in db.requests where r.departmant==

In C, what happens when the condition for a for loop aren't met at the beginning? [closed]

泄露秘密 提交于 2019-12-14 00:13:18
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . For example, what happens if I say: for(i = 2; i < 2; i++) Obviously, this is a useless for loop, but maybe i = a, and a is set by something else. So what happens in this case? 回答1: Neither iteration of the loop will be executed. In fact this loop (provided that the condition has no side effects) for(i = 2; i <

addition of one column with certain condition in another colum, like sumifs of excel in matlab [duplicate]

天大地大妈咪最大 提交于 2019-12-13 23:16:51
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: addition of one column with certain condition in another colum, like sumifs of excel here i want to count the no of 2nd column, with the same conditions in column 1, i m dictating here again, A=[1 2;2 3;3 4;3 2;4 3;5 4;5 2;6 3;7 2;8 3] now i want to count no of 2, 3 and 4 in column2, condition is that when column 1 ranges from 0 to 3, 3 to 6, 6 to 9, 9 to 12 like that. answer is like that ranges no2, no3, no4

subsetting a dataframe by a condition in R [duplicate]

限于喜欢 提交于 2019-12-13 23:16:05
问题 This question already has answers here : Filtering a data frame by values in a column [duplicate] (3 answers) Closed 8 months ago . I have the following data with the ID of subjects. V1 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 I want to subset all the rows of the data where V1 == 4. This way I can see which observations relate to subject 4. For example, the correct output would be 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24

Math string with no spaces?

☆樱花仙子☆ 提交于 2019-12-13 21:15:44
问题 import java.util.Scanner; public class Improved { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a number operaion number: "); int operand1 = Integer.parseInt(input.nextLine()); char expo1 = input.next().charAt(0); int operand2 = Integer.parseInt(input.nextLine()); System.out.println( operand1 + expo1 + operand2 + "="); if ( expo1 == '/' && operand2 == '0' ){ System.out.println("Cannot divide by zero"); } else if (expo1 == '-') {

R - How to sum objects in a column between an interval defined by conditions on another column

不羁岁月 提交于 2019-12-13 20:14:06
问题 This comes as an application to this question:Sum object in a column between an interval defined by another column What I would like to know is how to adjust the answer if I want to sum the values in B, for ((A[i+1]-A[i]==0) or (A[i+1]-A[i]==1) or (A[i]-A[i-1]==0) or (A[i]-A[i-1]==1)) where i is the row index, so basically sum B rows for A-s that have the same value +/- 1, but not sum the same row twice? I tried building a loop function but I get stuck when using row indices with data frames.