boolean

How to evaluate python string boolean expression using eval()?

依然范特西╮ 提交于 2021-02-08 07:47:37
问题 I get boolean expression like below : string = '!True && !(True || False || True)' I know eval('1+2') returns 3 . But when I am executing eval(string) it is throwing me error as in invalid syntax. Is there any other way I can execute the above expression? 回答1: None of ! , && and || are valid Python operators; eval() can only handle valid Python expressions. You'd have to replace those expressions with valid Python versions; presumably ! is not , && is and , and || is or , so you could just

Python - Check if a number is a square

孤街浪徒 提交于 2021-02-08 04:36:38
问题 I wrote a function that returns whether a number input is a square or not def is_square(n): if n<1: return False else: for i in range(int(n/2)+1): if (i*i)==n: return True else: return False I am confident that this code works. But when I did the test cases, example: test.expect( is_square( 4)) , it says that the value is not what was expected. 回答1: Your function doesn't actually work, as it will immediatley return False on the first non-square-root found. Instead you will want to modify your

get index of the first block of at least n consecutive False values in boolean array

我是研究僧i 提交于 2021-02-07 13:54:11
问题 I have a numpy boolean array w=np.array([True,False,True,True,False,False,False]) I would like to get the index of the first time there are at n_at_least false values. For instance here `n_at_least`=1 -> desired_index=1 `n_at_least`=3 -> desired_index=4 I have tried np.cumsum(~w) which does increase every time a False value is encountered. However, when True is encountered the counter is not starting from 0 again so I only get the total count of False elements rather than the count of the

In python, is there some kind of mapping to return the “False value” of a type?

喜夏-厌秋 提交于 2021-02-07 04:41:08
问题 I am looking for some kind of a mapping function f() that does something similar to this: f(str) = '' f(complex) = 0j f(list) = [] Meaning that it returns an object of type that evaluates to False when cast to bool . Does such a function exist? 回答1: No, there is no such mapping. Not every type of object has a falsy value, and others have more than one. Since the truth value of a class can be customized with the __bool__ method, a class could theoretically have an infinite number of (different

Java Event Listener to detect a variable change

和自甴很熟 提交于 2021-02-06 08:51:04
问题 I cannot seem to find an answer anywhere to my question. Is there any event listener which can detect the changing of a boolean or other variable and then act on it. Or is it possible to create a custom event listener to detect this? Please I cannot seem to find a solution to this anywhere and I found this website explaining how to create custom events 回答1: Just like you need to create an event listener, you will also need to create the event firer -- since there is nothing automatic that

Java Event Listener to detect a variable change

让人想犯罪 __ 提交于 2021-02-06 08:50:33
问题 I cannot seem to find an answer anywhere to my question. Is there any event listener which can detect the changing of a boolean or other variable and then act on it. Or is it possible to create a custom event listener to detect this? Please I cannot seem to find a solution to this anywhere and I found this website explaining how to create custom events 回答1: Just like you need to create an event listener, you will also need to create the event firer -- since there is nothing automatic that

Converting an empty string into nil in Ruby

我与影子孤独终老i 提交于 2021-02-05 20:20:32
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I

Converting an empty string into nil in Ruby

試著忘記壹切 提交于 2021-02-05 20:20:23
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I

Converting an empty string into nil in Ruby

夙愿已清 提交于 2021-02-05 20:20:11
问题 I have a string called word and a function called infinitive such that word.infinitive would return another string on some occasions and an empty string otherwise I am trying to find an elegant ruby one line expression for the code-snippet below if word.infinitive == "" return word else return word.infinitive Had infinitive returned nil instead of "", I could have done something like (word.infinitive or word) But since it does not, I can't take advantage of the short-circuit OR Ideally I

Pandas concat flips all my values in the DataFrame

断了今生、忘了曾经 提交于 2021-02-05 12:23:41
问题 I have a dataframe called 'running_tally' list jan_to jan_from 0 LA True False 1 NY False True I am trying to append new data to it in the form of a single column dataframe called 'new_data' list 0 HOU 1 LA I concat these two dfs based on their 'list' column for further processing, but immediately after I do that all the boolean values unexpectedly flip. running_tally = pd.concat([running_tally,new_data]).groupby('list',as_index=False).first() the above statement will produce: list jan_to jan