【问题标题】:What does the operator //= do in Python? [duplicate]运算符 //= 在 Python 中做了什么? [复制]
【发布时间】:2020-06-20 21:16:55
【问题描述】:

我有这个代码来计算n的素数

def factors(n):
    factorlist = []
    k = 2
    while k<=n:
        while n%k==0:
            factorlist.append(k)
            n //= k
        k += 1
    return factorlist

n //= k 行在 while 循环中做了什么?

我已经寻找运营商//=,但我没有找到任何东西。我想这是确保k 是素数的原因。但是怎么做呢?

【问题讨论】:

标签: python loops while-loop


【解决方案1】:

运算符将n 除以k(整数除法,余数)并将结果分配给n

【讨论】:

    【解决方案2】:
    n //= k
    

    相同
    n = n // k
    

    在哪里

    //
    

    将运算符两边的两个数相除,给出不带小数部分的正确答案。

    【讨论】:

      【解决方案3】:

      操作符“//”是一个楼除法操作符,它是一个结果为整数的除法,总是调整在数轴的左侧。

      例子:

      19//10 = 1

      19/10 = 1.9。

      通常,这个整数调整会导致数字 2,因为它比 1 更接近 1.9,但 楼层划分 会忽略这一点。 p>

      n //= k
      

      就像

      n = n // k
      

      【讨论】:

        【解决方案4】:

        您可以在 Python 中输入 help('//') 以获取更多信息。但这是出现的部分:

        Operator precedence
        *******************
        
        The following table summarizes the operator precedence in Python, from
        lowest precedence (least binding) to highest precedence (most
        binding).  Operators in the same box have the same precedence.  Unless
        the syntax is explicitly given, operators are binary.  Operators in
        the same box group left to right (except for exponentiation, which
        groups from right to left).
        
        Note that comparisons, membership tests, and identity tests, all have
        the same precedence and have a left-to-right chaining feature as
        described in the Comparisons section.
        
        +-------------------------------------------------+---------------------------------------+
        | Operator                                        | Description                           |
        |=================================================|=======================================|
        | "lambda"                                        | Lambda expression                     |
        +-------------------------------------------------+---------------------------------------+
        | "if" – "else"                                   | Conditional expression                |
        +-------------------------------------------------+---------------------------------------+
        | "or"                                            | Boolean OR                            |
        +-------------------------------------------------+---------------------------------------+
        | "and"                                           | Boolean AND                           |
        +-------------------------------------------------+---------------------------------------+
        | "not" "x"                                       | Boolean NOT                           |
        +-------------------------------------------------+---------------------------------------+
        | "in", "not in", "is", "is not", "<", "<=", ">", | Comparisons, including membership     |
        | ">=", "!=", "=="                                | tests and identity tests              |
        +-------------------------------------------------+---------------------------------------+
        | "|"                                             | Bitwise OR                            |
        +-------------------------------------------------+---------------------------------------+
        | "^"                                             | Bitwise XOR                           |
        +-------------------------------------------------+---------------------------------------+
        | "&"                                             | Bitwise AND                           |
        +-------------------------------------------------+---------------------------------------+
        | "<<", ">>"                                      | Shifts                                |
        +-------------------------------------------------+---------------------------------------+
        | "+", "-"                                        | Addition and subtraction              |
        +-------------------------------------------------+---------------------------------------+
        | "*", "@", "/", "//", "%"                        | Multiplication, matrix                |
        |                                                 | multiplication, division, floor       |
        |                                                 | division, remainder [5]               |
        +-------------------------------------------------+---------------------------------------+
        | "+x", "-x", "~x"                                | Positive, negative, bitwise NOT       |
        +-------------------------------------------------+---------------------------------------+
        

        另见:https://en.wikipedia.org/wiki/Floor_and_ceiling_functions

        【讨论】:

          猜你喜欢
          • 2013-09-15
          • 2013-11-03
          • 2020-08-26
          • 2013-07-10
          • 1970-01-01
          • 2017-12-20
          • 2013-03-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多