如何扭转不平等陈述?

Kev*_*eia 2 python if-statement

如果将不等式乘以负数,则必须反转不等式的方向.

例如:

  • 1 <x <6(1)
  • -1> -x> -10(2)

如果x = 6,则与等式(1)和(2)一致.

有没有办法将不等式语句乘以一行中的整数来反转符号?

从实际的角度来看,我试图从TBLASTN结果中提取DNA /蛋白质序列.有股+1和-1,该条件语句后的操作是相同的.

# one-liner statement I would like to implement
if (start_codon <= coord <= stop_codon)*strand:
    # do operation

# two-liner statement I know would work
if (start_codon <= coord <= stop_codon) and strand==1:
    # do operation
elif (start_codon >= coord >= stop_codon) and strand==-1:
    # do operation
Run Code Online (Sandbox Code Playgroud)

sch*_*ggl 5

您可以根据strand值选择下限和上限.这假定strand是总是要么1-1与使得使用bool作为一个int在Python子类,以便TrueFalse可用于索引到对:

cdns = (start_codon, stop_codon)
if (cdns[strand==-1] <= coord <= cdns[strand==1]):
    # Python type coercion (True -> 1, False -> 0) in contexts requiring integers
Run Code Online (Sandbox Code Playgroud)