fifo queue
hi, how would u improve this code? class queue: def __init__(self, size=8): self.space = size self.data = [None]*self.space self.head = 0 self.tail = 0 self.len = 0 def __len__(self): return self.len def push(self, x): if self.len==self.space: self.data.extend( self.data[:self.tail] ) self.data.extend( [None]* (self.space-self.tail) ) self.tail+=self.space self.space*=2 self.data[self.tail]=x self.tail+=1 if self.tail==self.space: self.tail=0 self.len+=1 def pop(self): if self.len: elem = self.data[self.head] self.head+=1 if self.head==self.space: self.head=0 return elem def top(self): if self.len==0: raise Exception, 'queue is empty' return self.data[self.head] thx in adv. -- http://mail.python.org/mailman/listinfo/python-list
Re: where function
On 18 Mar, 15:19, [EMAIL PROTECTED] wrote: Is there a function in Python analogous to the where function in IDL? x=[0,1,2,3,4,2,8,9] print where(x=2) output: [2,5] You can try this: print filter( lambda x: a[x]==2, range(len(a))) However it's not the best solution... -- http://mail.python.org/mailman/listinfo/python-list
Re: a good algo to do this
hi, try this: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/190465 cheers. -- http://mail.python.org/mailman/listinfo/python-list
Re: duplicate items in a list
http://groups.google.com/group/comp.lang.python/browse_thread/thread/32e545ebba11dd4d/49a9f0cc799cc1f1#49a9f0cc799cc1f1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing duplicates from a list
Rubinho napisal(a): I've a list with duplicate members and I need to make each entry unique. hi, other possibility (my newest discovery:) ) a = [1,2,2,4,2,1,3,4] unique = d.fromkeys(a).keys() unique [1, 2, 3, 4] regards przemek -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing duplicates from a list
there wasn't any information about ordering... maybe i'll find something better which don't destroy original ordering regards przemek -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing duplicates from a list
i suppose this one is faster (but in most cases efficiency doesn't matter) def stable_unique(s): e = {} ret = [] for x in s: if not e.has_key(x): e[x] = 1 ret.append(x) return ret cheers, przemek -- http://mail.python.org/mailman/listinfo/python-list
Re: Removing duplicates from a list
thanks, nice job. but this benchmark is pretty deceptive: try this: (definition of unique2 and unique3 as above) import timeit a = range(1000) t = timeit.Timer('unique2(a)','from __main__ import unique2,a') t2 = timeit.Timer('stable_unique(a)','from __main__ import stable_unique,a') t2.timeit(2000) 1.8392596235778456 t.timeit(2000) 51.52945844819817 unique2 has quadratic complexity unique3 has amortized linear complexity what it means? it means that speed of your algorithm strongly depends on len(unique2(a)). the greater distinct elements in a the greater difference in execution time of both implementations regards przemek -- http://mail.python.org/mailman/listinfo/python-list