Efficient mechanism for str.startswith on a set.

2006-01-10 Thread Brian Cole
I need to iterate through a file, checking whether each line 'startswith()' a string that belongs to a set. Normally, the most efficient way I would do this would be: strs=set(['foo','bar']) for line in file: if line.strip() in strs: print line However, for this case I need to do a st

Re: Efficient mechanism for str.startswith on a set.

2006-01-10 Thread Fredrik Lundh
Brian Cole wrote: >I need to iterate through a file, checking whether each line > 'startswith()' a string that belongs to a set. > > Normally, the most efficient way I would do this would be: > strs=set(['foo','bar']) > for line in file: >if line.strip() in strs: >print line > > Howeve

Re: Efficient mechanism for str.startswith on a set.

2006-01-10 Thread Paul Rubin
Brian Cole <[EMAIL PROTECTED]> writes: > This is obviously the least efficient manner to do this as I'll always > be iterating over the entire 'strs'. I know I could make a binary tree > out of 'strs' but that's a little more work that don't have time to do > today. I know there should be something