【问题标题】:Why is 0.0 not being counted?为什么不计算 0.0?
【发布时间】:2019-09-26 03:04:36
【问题描述】:

尝试制作一个将零放在末尾的列表。它忽略了0.0,它也需要放在最后作为0。为什么会这样?

尝试使用float(0)/ 0.0。如果我将它更改为不同的整数而不是 0.0,它会起作用。

期望的输出[9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

def move_zeros(array):
    count = 0
    for x in array: #counts how many zeros
        if x is 0 or float(0):
            count+=1
    array = [x for x in array if x is not 0] # removes all zeros
    array = [x for x in array if x is not float(0)]
    for y in range(count):
        array.append(0) #tacks zero to the end of list

    print(array)

move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9])

预计可以工作,但它忽略了0.0

【问题讨论】:

    标签: arrays python-3.x sorting


    【解决方案1】:

    如果两个变量指向同一个对象,则返回True,如果两个变量引用的对象相等,则==。

    有关is== 之间区别的更详细说明,请参阅this 优秀答案。

    正如其他答案中所述,您应该在您的情况下使用 ==!=,因为您正在检查 values 是否相等,而不是两个对象是否是相同的对象记忆。

    这是您的代码,已修复错误:

    def move_zeros(array):
        count = 0
        result = []
        for x in array: #counts how many zeros
            if x == 0 or x == float(0):
                count+=1
    
            elif x is not False and x is not None:
                result.append(x)
    
        for y in range(count):
            result.append(0) #tacks zero to the end of list
    
        print(result)
    
    move_zeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9])
    

    【讨论】:

    • 列表中也可能有 False 抱歉忘记包括在内。这就是我使用 is 而不是 == 的原因。但是,是的,这个答案很有帮助。谢谢
    • @stumpedstump 添加了一个更新来解决这个问题。我也刚刚意识到你真的不需要列表推导。相反,您应该检查初始 for x in array 循环中的条件。这样您只需迭代一次,而不是 3 次以上。
    【解决方案2】:

    您不应该使用is 来执行算术比较,并且您不能像这样使用or 组合两个条件。更改条件如下:

    if x == 0:
    

    类似地修复列表推导中的条件(x != 0 而不是 x is not 0)。

    【讨论】:

      【解决方案3】:

      有几个问题:

      1. is 评估这两个对象是否是内存中的同一个对象。你想使用==
      2. if x is 0 or float(0) 不是有效代码。请改用if x is 0 or x == float(0)if x in (0, float(0)。不过,您实际上不需要区分 0float(0)。只需使用if x == 0if not x
      3. 在您的列表推导中也出现了同样的问题。使用x != 0 或简单地使用x 而不是is not

      【讨论】:

        【解决方案4】:

        关于or的说明:

        • x == 0 or x == float(0) 工作
        • x in [0, float(0)] 也有效且更简单
          • [x for x in array if x != 0] & [x for x in array if x != float(0)] 可以替换为[x for x in array if x not in [0, float(0)]]

        简化函数

        def move_zeros(array):
            zc = array.count(0)
            array = [x for x in array if x != 0] # removes all zeros
            array.extend([0 for _ in range(zc)])
        
            return array
        
        test = [9, 0.0, 0, 9, 1, 2, 0, 1, 0, 1, 0.0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
        
        y = move_zeros(test)
        
        print(y)
        
        >>> [9, 9, 1, 2, 1, 1, 3, 1, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        

        验证ytest

        from collections import Counter
        
        print(test.count(0))
        >>> 10
        
        print(y.count(0))
        >>> 10
        
        print(len(test))
        >>> 20
        
        print(len(y))
        >>> 20
        
        test_dict = Counter(test)
        print(test_dict)
        >>> Counter({9: 4, 0.0: 10, 1: 4, 2: 1, 3: 1})
        
        y_dict = Counter(test)
        print(y_dict)
        >>> Counter({9: 4, 0.0: 10, 1: 4, 2: 1, 3: 1})
        

        或者:

        test = [9, 0.0, 0, 9, 1, 2, 0, 1, 0, 1, 0.0, 3, 0, 1, 9, 0, 0, 0, 0, 9]
        
        test_sorted = test.sort(reverse=True)
        print(test_sorted)
        >>> [9, 9, 9, 9, 3, 2, 1, 1, 1, 1, 0.0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-27
          • 1970-01-01
          • 2018-06-20
          • 2022-11-18
          • 1970-01-01
          • 2018-07-29
          • 2013-12-12
          • 1970-01-01
          相关资源
          最近更新 更多