python - Push all zeros to one side of the list -


this question has answer here:

hello i'm trying push zeros in list 1 side without altering rest of it:
[0,2,0,0,9] -> [0,0,0,2,9]
[3,4,0,1,0] -> [0,0,3,4,1]

my solution:

def getnonzeros(self, tup):     in tup:         if != 0:             yield i;  def pushzeros(self, tup, side):     if side==side.end:         return [i in self.getnonzeros(tup)] + [0]*tup.count(0);     else:         return [0]*tup.count(0) + [i in self.getnonzeros(tup)]; 

and error in ipython:

error: internal python error in inspect module. below traceback 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, original traceback can not constructed. 

since python sorts intrinsically stable, simple this

>>> sorted([0,2,0,0,9], key=lambda x: x != 0) [0, 0, 0, 2, 9] >>> sorted([3,4,0,1,0], key=lambda x: x != 0) [0, 0, 3, 4, 1] 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -