[CentralOH] Handy function to decimate a list of numbers (get every Nth item)
david.car7 at gmail.com
Thu Mar 19 01:22:59 CET 2009
Yeah, Marks is the fast way to do it.
On Wed, Mar 18, 2009 at 5:02 PM, Mark Erbaugh <mark at microenh.com> wrote:
> On Wed, 2009-03-18 at 16:48 -0400, Bryan Harris wrote:
> > Hey all, I came up with this little snippet to get every Nth line in a
> > Google didn't turn anything up in the first few pages. Does anybody know
> > better way? This seems like the kind of thing python would be able to do
> > automatically using the % (modulo) symbol or something.
> > Code:
> > def decimate(array,points):
> > if type(points) != type(1): #line 1.5 doesn't mean anything!
> > raise Error, "number of points should be an integer"
> > if len(array)/2<=points: #Not worth doing for an array this small
> > return array
> > new_array=
> > #get the line numbers rather than stepping through the entire list
> > interval=int(len(array)/points)
> > line_numbers = range(0,len(array),interval)
> > for each in line_numbers:
> > new_array=new_array + [array[each]]
> > return(new_array)
> How about array[::points]?
> array = ['Line %d] % i for i in range(100)]
> ['Line 0', 'Line 10', ... 'Line 90']
> CentralOH mailing list
> CentralOH at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CentralOH