python列表元素零的移动,如何将python列表或numpy数组中的所有非零元素移到一侧?...

I'm going to do the following operation of a list or numpy array:

[0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0]

move all non-zeros to the right side:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]

How can I do this efficiently?

Thanks

============

Sorry I didn't make it clear, I need the order of non-zeros elements remains.

解决方案

Using NumPy:

>>> a = np.array([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])

>>> np.concatenate((a[a==0], a[a!=0]))

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7])

You can do this in O(N) time in Python as well by using a simple for-loop. But will take some extra memory which we can prevent in @grc's solution by using a.sort(key=bool):

>>> from collections import deque

#Using a deque

>>> def solve_deque(lst):

d = deque()

append_l = d.appendleft

append_r = d.append

for x in lst:

if x:

append_r(x)

else:

append_l(x)

return list(d) #Convert to list if you want O(1) indexing.

...

#Using simple list

>>> def solve_list(lst):

left = []

right = []

left_a = left.append

right_a = right.append

for x in lst:

if x:

right_a(x)

else:

left_a(x)

left.extend(right)

return left

>>> solve_list([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]

>>> solve_deque([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])

[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值