gpt4 book ai didi

python - 将所有零推到列表的一侧

转载 作者:行者123 更新时间:2023-11-28 19:39:28 25 4
gpt4 key购买 nike

<分区>

你好,我正在尝试将列表中的所有零推到一侧而不改变其余部分:
[0,2,0,0,9] -> [0,0,0,2,9]
[3,4,0,1,0] -> [0,0,3,4,1]

我的解决方案:

def getNonZeros(self, tup):
for i in tup:
if i != 0:
yield i;

def pushZeros(self, tup, side):
if side==Side.End:
return [i for i in self.getNonZeros(tup)] + [0]*tup.count(0);
else:
return [0]*tup.count(0) + [i for i in self.getNonZeros(tup)];

我在 ipython 中得到这个错误:

ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/IPython/core/ultratb.py", line 760, in structured_traceback
records = _fixed_getinnerframes(etb, context, tb_offset)
File "/usr/lib/python2.7/site-packages/IPython/core/ultratb.py", line 242, in _fixed_getinnerframes
records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
File "/usr/lib64/python2.7/inspect.py", line 1043, in getinnerframes
framelist.append((tb.tb_frame,) + getframeinfo(tb, context))
File "/usr/lib64/python2.7/inspect.py", line 1007, in getframeinfo
lines, lnum = findsource(frame)
File "/usr/lib64/python2.7/inspect.py", line 580, in findsource
if pat.match(lines[lnum]): break
IndexError: list index out of range

Unfortunately, your original traceback can not be constructed.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com