标签: conditional-statements

如何在Sqlalchemy中使用条件配置多对多

我用sqlalchemy 0.6.4.

我有2个课程:问题和标签,它们是多对多的.

class Question(Base):
    __tablename__ = "questions"

    id = Column(Integer, primary_key=True)
    deleted = Column(Boolean)
    ...
    tags = relationship('Tag', secondary=r_questions_tags)

class Tag(Base):
    __tablename__ = "tags"

    id = Column(BigInteger, primary_key=True)
    questions = relationship('Question', secondary=r_questions_tags)
Run Code Online (Sandbox Code Playgroud)

因此,tag.questions将所有问题都归属于标签.

但是现在,既然Question有一个deleted专栏,我希望这样做:

class Tag(Base):
   ...

   # get non-deleted questions
   questions = relationship('Question', secondary=r_questions_tags, 
                           condition='Question.deleted==False')
   # get deleted questions
   deleted_questions = relationship('Question', secondary=r_questions_tags,
                           condition='Question.deleted==True')
Run Code Online (Sandbox Code Playgroud)

但不幸的是,没有这样的condition参数.我现在能做什么?

many-to-many sqlalchemy conditional-statements

1
推荐指数
1
解决办法
1280
查看次数

在R中进行子集,连接并计算多次重复

这是一个示例:

> tmp
    label   value1  value2
1   aa_x_x  xx      xx
2   bc_x_x  xx      xx
3   aa_x_x  xx      xx
4   bc_x_x  xx      xx
Run Code Online (Sandbox Code Playgroud)

如何计算所有重复标签的中位数(或更多,其他数据框列中的相应值),但仅考虑前两个字母(即"aa_1_1"和"aa_s_3"是相同的值)?标签列表是有限且可用的.

我看了一下aggregate,%in%,subsetsubstr,但我无法编译任何有用而简单.

这是我希望得到的:

> tmp.result
    label   median1 some.calculation2
1   aa      xx      xx
2   bc      xx      xx
3   aa      xx      xx
4   bc      xx      xx
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

string r subset conditional-statements dataframe

1
推荐指数
1
解决办法
194
查看次数

JavaScript条件执行和变量范围

我提前道歉,之前可能已经在StackOverflow上讨论了,我只是不知道这叫什么,所以我找不到满意的答案.

但是,我正在学习JavaScript和一本名为"Eloquent JavaScript"的书.在那里,我找到了以下代码,它反复提示用户输入他的名字,直到他这样做.

while (!input) {
    var input = prompt("Who are you?");
}
Run Code Online (Sandbox Code Playgroud)

我根本不明白为什么这实际上有效而不是引发错误.在评估条件表达式时,没有称为输入的变量.如果我理解正确,则无法进行评估,这通常会阻止进一步执行.在while循环体,它的声明,然后创建一个名为变量输入,仍在执行,但.

然而,这让我很焦虑,所以我尝试了这个:

while (!bool) {
    console.log("Hi");
    var bool = true;
}
Run Code Online (Sandbox Code Playgroud)

这甚至更奇怪.当涉及条件表达式时,它也是同样的问题:在评估条件之后,在循环体的范围内创建bool.其次,bool经常设置为true,但代码仍然执行一次,换句话说,Hi正在打印一次.

我很困惑,希望得到一些帮助.;)

javascript scope conditional-statements

1
推荐指数
1
解决办法
273
查看次数

列中的求和范围,另一列中的条件

如果另一列中的值等于某个数字,我会尝试对列中的范围值求和.例如(这是一个非常小的例子):

在此输入图像描述

我想总结C列中同一行中A中数字为"2"的所有值.例如,这个条件的总数应该是236.任何人都可以帮助我吗?我甚至没有得到任何代码.谢谢!

excel sum conditional-statements

1
推荐指数
1
解决办法
1710
查看次数

使用c中的条件声明变量的数据类型

我想根据C中的条件声明变量的数据类型.有可能吗?

我已经编写了一个使用整数数组实现堆栈的程序,我希望使用相同的代码来实现堆栈的字符,这只不过是用"char"替换一些"int",那么如何做到这一点?

我喜欢这样的东西,

if(x == 1)
#define DATATYPE int
else
#define DATATYPE char
Run Code Online (Sandbox Code Playgroud)

还有很多其他的东西,但没有任何效果.

c variables conditional-statements custom-data-type

1
推荐指数
1
解决办法
1017
查看次数

三元条件表达式中的奇怪NullPointerException

谁能告诉我为什么Java会在NullPointerException这里抛出一个?

Float x = <some condition> ? myObject.getSomeFloat() : 0.0f;
Run Code Online (Sandbox Code Playgroud)
  • 该方法getSomeFloat返回Float.
  • 更改0.0fnew Float(0)工作正常.

java nullpointerexception conditional-statements

1
推荐指数
1
解决办法
83
查看次数

C++相当于将一个lambda函数作为参数传递给python

在python中,您可以将lambda函数作为参数传递,如下所示:

class Thing(object):
    def __init__(self, a1, a2):
        self.attr1 = a1
        self.attr2 = a2

class ThingList(object):
    def __init__(self):
        self.things = [Thing(1,2), Thing(3,4), Thing(1,4)]

    def getThingsByCondition(self, condition):
        thingsFound = []
        for thing in self.things:
            if condition(thing):
                thingsFound.append(thing)
        return thingsFound

things = tl.getThingsByCondition(lambda thing: thing.attr1==1)
print things
Run Code Online (Sandbox Code Playgroud)

有没有办法在C++中做类似的事情?我需要这样做,因为我想在vector对象中搜索满足特定条件的对象.


好吧,我试着像这样解决它:我应该提到我在向量中管理的"事物"是员工,我想找到满足某些条件的员工.

employee_vector getEmployeeByCondition(function<bool(const Employee&)> condition) {
    employee_vector foundEmployees;
    for (int i = 0; i < employees.size(); i++) {
        Employee e = employees.at(i);
        if (condition(e)) {
            foundEmployees.push_back(e);
        }
    }
    return foundEmployees;
}

employee_vector getEmployeeByBirthday(Date …
Run Code Online (Sandbox Code Playgroud)

c++ python lambda function conditional-statements

1
推荐指数
1
解决办法
566
查看次数

delphi为对象属性分配条件

我想以更漂亮的方式编写这段代码

if some_condition then
  checkbox.Checked := true
else
  checkbox.Checked := false;
Run Code Online (Sandbox Code Playgroud)

我想要类似的东西

checkbox.checked := boolean_value_of_condition_is_met;
Run Code Online (Sandbox Code Playgroud)

delphi conditional-statements

1
推荐指数
2
解决办法
148
查看次数

C:根据变量的值在for语句中使用不同的条件

所以我想实现一个for语句,其'length'和条件取决于我给它的number-of-entries变量的值.循环主要是读取文件.

例如,如果条目数的命令行输入是正整数,我希望循环运行的条目数迭代或直到达到文件的末尾; 如果number-of-entries的输入是-1,我希望循环运行直到文件结束.

有没有办法在不编写两次for循环的情况下执行此操作,每一个都嵌套在if语句中; 有更简洁的方法吗?问因为for循环中的语句是相同的; 唯一的区别是for参数中的条件.

以下是我知道我能做的事情:

if ( number_of_entries > 0 ) {
    for ( i = 0; i < number_of_entries; i++ ){
        // set of for statements
        // check to see if end of file is reached
        // this case stops when i reaches number_of_entries or EOF
    }
}

else if ( number_of_entries < 0 ) {
    for ( i = 0; i > number_of_entries; i++ ){
        // identical set of for statements
        // check to see if …
Run Code Online (Sandbox Code Playgroud)

c for-loop if-statement conditional-statements

1
推荐指数
1
解决办法
117
查看次数

Most pythonic way to toggle a variable between two values

I have a variable, p, that is the output of a regular expression match and is guaranteed to be either 'true' or 'false' in any mixed case. If 'true' I want it to be 'FALSE' and if 'false' I want it to be 'TRUE'. The end result is always uppercase.

I have thought of the following four methods. Which is the most pythonic or is there a better one?

p=['TRUE','FALSE'][eval(p.capitalize())]
p=(not eval(p.capitalize())).__repr__().upper()
p='FALSE' if eval(p.capitalize()) else 'TRUE'
p={'TRUE':'FALSE','FALSE':'TRUE'}[p.upper()]
Run Code Online (Sandbox Code Playgroud)

python string conditional-statements

1
推荐指数
1
解决办法
56
查看次数