def move_zero(lst):
"""
Given a list of integers, moves all non-zero numbers to the beginning of the list and
moves all zeros to the end of the list. This function returns nothing and changes the given list itself.
For example:
- After calling move_zero([0,1,0,2,0,3,0,4]), the given list should be [1,2,3,4,0,0,0,0] and the function returns nothing
- After calling move_zero([0,1,2,0,1]), the given list should be [1,2,1,0,0] and the function returns nothing
- After calling move_zero([1,2,3,4,5,6,7,8]), the given list should be [1,2,3,4,5,6,7,8] and the function returns nothing
- After calling move_zero([]), the given list should be [] and the function returns nothing
"""
c = 0
for i in range (0,len(lst),1):
if (lst[i] == 0):
lst.pop(i)
c = 1
for i in range(0,c):
lst.append(0)
uj5u.com熱心網友回復:
這將使您更容易理解和其他人閱讀:
基本上收集所有的數字和零,然后添加串列。
def move_zero(lst):
zeros = []
nums = []
for num in lst:
if num == 0:
zeros.append(num)
else:
nums.append(num)
return nums zeros
print(move_zero([1, 2, 4, 0, 4, 0, 4, 6, 0, 1]))
輸出:
[1, 2, 4, 4, 4, 6, 1, 0, 0, 0]
uj5u.com熱心網友回復:
問題是什么?
您正在從串列中洗掉元素,使用list.pop
while 迭代,但您沒有考慮到串列中的元素(具有更大的索引)已更改索引的事實。
你怎么能接近解決方案?
為了與您的代碼保持一致,有必要在使用該list.pop
方法之前減去洗掉的元素總數。
注意:在功能上,我放了一個
代碼
這是代碼;如評論中所述,該函式正在回傳,并且正在就地None
修改串列。
另外,我在最后轉換了 for 回圈,從
for i in range(0,c):
lst.append(0)
對此代碼,使用生成器運算式,結合list.extend
方法:
lst.extend(0 for _ in range(c))
這是我想出的代碼實作:
def move_zero(lst):
"""
Given a list of integers, moves all non-zero numbers to the beginning of the list and
moves all zeros to the end of the list. This function returns nothing and changes the given list itself.
For example:
- After calling move_zero([0,1,0,2,0,3,0,4]), the given list should be [1,2,3,4,0,0,0,0] and the function returns nothing
- After calling move_zero([0,1,2,0,1]), the given list should be [1,2,1,0,0] and the function returns nothing
- After calling move_zero([1,2,3,4,5,6,7,8]), the given list should be [1,2,3,4,5,6,7,8] and the function returns nothing
- After calling move_zero([]), the given list should be [] and the function returns nothing
"""
c = 0
for i in range (0,len(lst),1):
if not lst[i-c]:
lst.pop(i-c)
c = 1
lst.extend(0 for _ in range(c))
print(lst)
move_zero([1,2,0,3,0])
我還建議遍歷串列的副本:
def move_zero(lst):
"""
Given a list of integers, moves all non-zero numbers to the beginning of the list and
moves all zeros to the end of the list. This function returns nothing and changes the given list itself.
For example:
- After calling move_zero([0,1,0,2,0,3,0,4]), the given list should be [1,2,3,4,0,0,0,0] and the function returns nothing
- After calling move_zero([0,1,2,0,1]), the given list should be [1,2,1,0,0] and the function returns nothing
- After calling move_zero([1,2,3,4,5,6,7,8]), the given list should be [1,2,3,4,5,6,7,8] and the function returns nothing
- After calling move_zero([]), the given list should be [] and the function returns nothing
"""
c = 0
for i,x in enumerate(lst.copy()):
if not x:
lst.pop(i-c)
c = 1
lst.extend(0 for _ in range(c))
print(lst)
move_zero([1,2,0,3,0])
輸出:
[1,2,3,0,0]
uj5u.com熱心網友回復:
def move_zero(lst):
zeros = [] # a list to store all the zeros
for num in lst:
if num == 0:
zeros.append(0)
lst.remove(0) # remove this zero from the list for now
lst = lst zeros # append the zeros to the end of the list
return lst
print(move_zero([1, 2, 4, 0, 4, 0, 4, 6, 0, 1]))
輸出:[1, 2, 4, 4, 4, 6, 1, 0, 0, 0]
不是最有效的方法,但它會完成這項作業。希望這可以幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/506826.html