我是python的新手,我需要简化这个检查器.我怎样才能改变:
...
if c == '>' and ( prevTime > currentTime ):
c2 = True
elif c == '>=' and ( prevTime >= currentTime ):
c2 = True
...
Run Code Online (Sandbox Code Playgroud)
类似于:
if prevTime | condition | currentTime:
doSomething()
Run Code Online (Sandbox Code Playgroud)
我曾尝试使用evaluate或compile,但在创建字符串期间,datetime对象与string之间存在转换(str在datetime对象上).例如:
>>> 'result = %s %s %s' % (datetime.now(), '>', datetime.utcfromtimestamp(41))
'result = 2011-04-07 14:13:34.819317 > 1970-01-01 00:00:41'
Run Code Online (Sandbox Code Playgroud)
它无法比较.
有人可以帮我弄这个吗?下面的工作示例:
def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
def checkCondition( condition ):
#condition format
#tuple ( (oldEvent, newEvent), …Run Code Online (Sandbox Code Playgroud) 经过几天的苦思冥想,我选择问这个问题。我有if多个条件的声明:
//var current is array of arrays of integers
if((current[rot][0] + x)<blocks.length
&& (current[rot][1] + x)<blocks.length
&& (current[rot][2] + x)<blocks.length
&& (current[rot][3] + x)<blocks.length
&& !$(blocks[current[rot][0]+x]).hasClass("blockLand")
&& !$(blocks[current[rot][1]+x]).hasClass("blockLand")
&& !$(blocks[current[rot][2]+x]).hasClass("blockLand")
&& !$(blocks[current[rot][3]+x]).hasClass("blockLand"))
{
//something to happen here ONCE!
}
Run Code Online (Sandbox Code Playgroud)
因为我希望一旦我认为我无法使用,内部就会发生一些事情for loop。所以我的问题是:有没有可能的方法来减少条件数量?如何?
PS:是的,我发现我可以在里面使用flag( ) 并在这个外面的另一个true/false地方做我的事情- 但我认为这并不总是有效,因为对于每个循环,标志都会不同。ifif
编辑:
我在最后删除了令人困惑的评论,并添加了一个额外的列pl_id我认为无关紧要(由于我明显缺乏对group by工作的理解).在尝试发布的建议时,我遇到了导致MAX/Group By导致的问题
| name | license_plate | pl_id | start_date |
| aaron | AA-AA-22 | 3 | 2016-1-1 |
Run Code Online (Sandbox Code Playgroud)
我期望的地方:
| name | license_plate | pl_id | start_date |
| aaron | AA-AA-22 | 4 | 2016-1-1 |
Run Code Online (Sandbox Code Playgroud)
因此,不知何故,车牌号AA-AA-22的最大日期,即2016-1-1,出现在pl_id = 3的记录中,为什么结果会返回带有pl_id 4日期的记录? 结束编辑
我有一个列表:
我需要一个查询,它为每个牌照返回一条记录,其最大值为start_date或start_date为空.但是空的start_date应该优先于最大的start_date.
每个牌照保证最多有一个条目,start_date = null,但可以有多个条目,有效的'start_date'-s.
例如,在下表中执行时:
| name | license_plate | pl_id | start_date |
| aaron | AA-AA-11 | 1 | 2015-1-1 |
| aaron | AA-AA-11 | …Run Code Online (Sandbox Code Playgroud) 共同代码:
class ThingA end
class ThingB end
class ThingC end
Run Code Online (Sandbox Code Playgroud)
为了设置上述类型的条件检查,我使用了基本的"if!..."结构,它可以产生预期的准确结果.
示例代码if!...:
obj = ThingA.new
puts 'yes it is a ThingC' if !obj.is_a?(ThingA) && !obj.is_a?(ThingB) # works ok
# stdout => nothing
obj = ThingB.new
puts 'yes it is a ThingC' if !obj.is_a?(ThingA) && !obj.is_a?(ThingB) # works ok
# stdout => nothing
obj = ThingC.new
puts 'yes it is a ThingC' if !obj.is_a?(ThingA) && !obj.is_a?(ThingB) # works ok
# stdout => yes it is a ThingC
Run Code Online (Sandbox Code Playgroud)
考虑到"除非"是基本"if!..."构造的更具描述性的替代,我使用"除非"来实现上述.
示例代码除非:
obj = …Run Code Online (Sandbox Code Playgroud) 我正在使用codeigniter MVC,我想允许用户删除他们的页面.
因此,如果page_id = 1要传递以user_id =1进行删除,则只应删除表中user_id = 1和page_id = 1的行.
这是我的model_page.php代码到目前为止,但user_id 1的所有页面都在删除
function delete_user_page($user_id, $page_id){
$query = $this->db->where('user_id', $user_id and 'page_id', $page_id);
return $this->db->delete('mysql_table');
}
Run Code Online (Sandbox Code Playgroud)
谢谢
下面的数据存在于“ examSheet”集合中
{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"40","m":"28","c":"38"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"40","m":"50","c":"48"}]}
{"name":"a1", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"45","m":"42","c":"18"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_T", "marks":[{"p":"25","m":"30","c":"50"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"halfyr_P", "marks":[{"p":"41","m":"48","c":"28"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_T", "marks":[{"p":"30","m":"48","c":"24"}]}
{"name":"a2", "std":"9", "year":"2017", "exam":"annual_P", "marks":[{"p":"35","m":"08","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_T","marks":[{"p":"45","m":"40","c":"50"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"40","m":"28","c":"38"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_T", "marks": [{"p":"40","m":"50","c":"48"}]}
{"name":"b1", "std":"10", "year":"2017", "exam":"annual_P", "marks": [{"p":"45","m":"42","c":"18"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_T", "marks": [{"p":"25","m":"30","c":"50"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"halfyr_P", "marks": [{"p":"41","m":"48","c":"28"}]}
{"name":"b2", "std":"10", "year":"2017", "exam":"annual_T", …Run Code Online (Sandbox Code Playgroud) 这是一个检查共素对的程序.
我正在尝试编写一个程序来接收整数输入,直到用户输入0,这很容易在数组的帮助下解决(我已经用数组做了)因为一次只能读取一个值并检查.使用数组它只是:
for(i = 0; i < n-1; i++)
然后比较v[i]和v[i+1]
我试图在没有数组的情况下应用这种精确的检查算法,但是,读取两个值并比较它们,不知何故,只有当我多次输入0,有时两次,有时三次时,循环才会结束.
#include <stdio.h>
int gcd1(int a, int b) //function containing Euclid's algorithm
{
while (b != 0)
{
int temp = a%b;
a = b;
b = temp;
}
return a;
}
int main(int argc, char * argv[])
{
int num1, num2; /* both of these vars would otherwise have a non-zero
value if I was using Try Nr.1 written in bold below was applied */
int …Run Code Online (Sandbox Code Playgroud) 我对“if”语句中的“or”和“and”操作有点困惑。在此if(condition_1 || condition_2)操作中,当 OR 运算符检查两个条件及其检查它们的行为时。同样if(condition_1 && condition_2)在此操作中,当 && 运算符检查条件及其检查条件的行为时。
“OR”和“AND”运算如何检查两个或多个条件?
weatherType = raw_input('Enter a weather type: ')
while (weatherType != "WINDDIRECTION") or (weatherType != "WINDSPEED") or (weatherType != "AIRTEMPERATURE") or (weatherType != "WAVEHEIGHT") or (weatherType != "AIRPRESSURE"):
print "Sorry, invalid input. Please enter AIRTEMPERATURE, AIRPRESSURE, WAVEHEIGHT, WINDSPEED, or WINDDIRECTION for a city and either WINDDIRECTION, WINDSPEED, or AIRTEMPERATURE for an off shore bouy"
weatherType = raw_input('Enter a weather type: ')
Run Code Online (Sandbox Code Playgroud)
好了,所以用这个循环中,我试图让用户输入要么WINDDIRECTION,WINDSPEED,AIRTEMPERATURE,WAVEHEIGHT,或AIRPRESSURE.但是,即使用户输入这5个选项中的1个,我的代码仍然会进入while循环.我不知道发生了什么事.我知道我可以使用for循环(对于["WINDDIRECTION",....]中的x)但是for循环只能查看他们的输入是否是第一次是正确的,如果他们再输入错误的答案代码将继续
我需要同时为i和j执行两个条件.
我的条件: for(i=1*counter; i<= len*7*counter; i++)
条件为j: for(j=len*7*counter; j>=1*counter; j--)
然后,当这两个条件都成立时,我需要执行 bin[i-1]=temp[j-1];
写这个的正确方法是什么?
注意:这不是嵌套循环.
这是正确的方法吗?
for(i=1*counter && j=a*counter; i<=a*counter && j>=1*counter; i++ && j--)
编写带有多个||的if语句的简洁方法是什么 和&&在C?
我想只执行一个printf语句,如果1,2,4或6 AND b = 8和c = 10,我可以将所有这些条件放在同一个if语句中吗?
例如.我可以这样写:
if ((a = 1||2||4||6) && b == 8 && c == 10)
//do something
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用......
c ×3
if-statement ×3
for-loop ×2
mysql ×2
python ×2
c++ ×1
codeigniter ×1
group-by ×1
input ×1
javascript ×1
jquery ×1
logic ×1
match ×1
mongodb ×1
php ×1
ruby ×1
while-loop ×1