【发布时间】: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