英文:
How can i move just Zero to the end of my list and not False in python
问题
我的结果是 [1, 1, 2, 1, 3, 'a', False, 0, 0]
,我不希望False移动,我的程序将False视为0。
英文:
def move_zeros(array):
for element in array:
if element == 0 and type(element) is not bool:
array.append(array.pop(array.index(element)))
return array
print(move_zeros([False,1,0,1,2,0,1,3,"a"]))
My result is [1, 1, 2, 1, 3, 'a', False, 0, 0]
I don't want False to move, my program sees False as 0.
答案1
得分: 4
这是因为你在循环遍历列表的同时对其进行操作,以及你正确识别到的问题,即False == 0
和0 == 0
在Python中都是True
。处理这个问题的一种方法是使用以下方法,使用is
而不是==
来检查是否等于0
:
def move_zeros(a):
return [x for x in a if x is not 0] + [x for x in a if x is 0]
print(move_zeros([False,1,0,1,2,0,1,3,"a"]))
输出:
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
请注意,通常情况下,使用is
比较整数是不安全的,因为范围在(-5, 256)之外的整数具有不同的标识(参见此问题)。然而,在我们的情况下,我们只是使用0
,所以上面的解决方案是安全的。
英文:
This is coming about because you're operating on the list while looping over it, as well as the issue that you've correctly identified, that False == 0
and 0 == 0
both are True
in Python. One way to deal with this is the following, using is
instead of ==
to check equality to 0
:
def move_zeros(a):
return [x for x in a if x is not 0] + [x for x in a if x is 0]
print(move_zeros([False,1,0,1,2,0,1,3,"a"]))
Output:
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
Note that the use of is
to compare integers is not safe in general because integers outside of the range (-5, 256) have different ids from each other (see this question). In our case however, we are just using 0
, so the solution above is safe.
答案2
得分: 2
以下是翻译好的代码部分:
sorted(array, key=lambda x: x == 0 and type(x) is not bool)
sorted(array, key=lambda x: x == 0 and x is not False)
sorted(array, key=lambda x: 0 == x is not False)
英文:
Simple solution and showing that your check is actually ok:
>>> sorted(array, key=lambda x: x == 0 and type(x) is not bool)
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
Or compare with the False
singleton:
>>> sorted(array, key=lambda x: x == 0 and x is not False)
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
Or if you're feeling playful, chained comparisons:
>>> sorted(array, key=lambda x: 0 == x is not False)
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
答案3
得分: 1
这段代码实现了将“零”移到末尾的原地洗牌,空间复杂度为O(1),时间复杂度为O(n)。
def move_zeroes_to_end(nums):
n = len(nums)
non_zero = 0
for i in range(n):
if nums[i] != 0 or type(nums[i])==bool:
nums[non_zero],nums[i] = nums[i],nums[non_zero]
non_zero +=1
return nums
l=[False,1,0,1,2,0,1,3,"a"]
print(move_zeroes_to_end(l)) #输出:[False, 1, 1, 2, 1, 3, 'a', 0, 0]
英文:
This does the inplace shuffling of "zero" to the end, costing O(1) space and O(n) time complexity.
def move_zeroes_to_end(nums):
n = len(nums)
non_zero = 0
for i in range(n):
if nums[i] != 0 or type(nums[i])==bool:
nums[non_zero],nums[i] = nums[i],nums[non_zero]
non_zero +=1
return nums
l=[False,1,0,1,2,0,1,3,"a"]
print(move_zeroes_to_end(l)) #Output:[False, 1, 1, 2, 1, 3, 'a', 0, 0]
答案4
得分: 1
你可以使用pop()
和append()
来实现这个功能,主要通过一个“自由计数”循环,通常使用while
循环来完成:
def move_zeros(a):
l = len(a)
i = 0
while i < l:
if a[i] == 0:
a.append(a.pop(i))
l = l - 1
else:
i = i + 1
return a
print(move_zeros([False, 1, 0, 1, 2, 0, 1, 3, "a"]))
生成的输出会保持原始顺序:
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
英文:
You can do that with pop()
+append()
, just with a "free counting" loop, which is mainly a while
:
def move_zeros(a):
l=len(a)
i=0
while i<l:
if a[i] is 0:
a.append(a.pop(i))
l=l-1
else:
i=i+1
return a
print(move_zeros([False,1,0,1,2,0,1,3,"a"]))
Produces the output maintaining the order as:
> [False, 1, 1, 2, 1, 3, 'a', 0, 0]
答案5
得分: 1
这是我的实现,使用循环和计数器:
def move_zeros(array):
result, count = [], 0
for i in array:
if i == 0 and type(i) == int: count+= 1
else: result.append(i)
return result + [0]*count
输出
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
英文:
This is my implementation, using counter with loop:
def move_zeros(array):
result, count = [], 0
for i in array:
if i == 0 and type(i) == int: count+= 1
else: result.append(i)
return result + [0]*count
print(move_zeros([False,1,0,1,2,0,1,3,"a"]))
Output
[False, 1, 1, 2, 1, 3, 'a', 0, 0]
答案6
得分: 0
你可以将False保存为字符串。然后它不会被视为0。
将您的代码更改为:
print(move_zeros(["False", 1, 0, 1, 2, 0, 1, 3, "a"]))
英文:
You can save False as a String. Then it is not seen as 0.
change your code to:
print(move_zeros(["False",1,0,1,2,0,1,3,"a"]))
答案7
得分: -1
将以下内容翻译为中文:
type(element) is not bool
变为:
not element == False and not element == True
它更长,但它修复了你的问题。
英文:
Change:
type(element) is not bool
to:
not element == False and not element == True
It's longer, but it fixes your problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论