[Tutor] How to find a substring within a list of items
I have an object that contains about 3500 list items, each list containing various data, some strings and some floats, like so: ['D', 123.4,'This is a project description', 'type', 52.1,'title'] What is the easiest way to search this list for a given string? So I want to find out if this list contains the string 'scrip' anywhere within it (case insensitive and including being just part of a larger string). Incidentally, I'm using the xlrd module to read in a spreadsheet. I effectively want to quickly pull out a list of lines from that spreadsheet that contain that substring anywhere within them. Maybe there is a better/faster way I should be doing this? I'm trying to give employees here a better/faster way of filtering through the company project list rather than opening up excel and doing a find search each time. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to find a substring within a list of items
On Thu, Jan 13, 2011 at 12:10 PM, Richard Querin rfque...@gmail.com wrote: I have an object that contains about 3500 list items, each list containing various data, some strings and some floats, like so: ['D', 123.4,'This is a project description', 'type', 52.1,'title'] What is the easiest way to search this list for a given string? So I want to find out if this list contains the string 'scrip' anywhere within it (case insensitive and including being just part of a larger string). Incidentally, I'm using the xlrd module to read in a spreadsheet. I effectively want to quickly pull out a list of lines from that spreadsheet that contain that substring anywhere within them. Maybe there is a better/faster way I should be doing this? I'm trying to give employees here a better/faster way of filtering through the company project list rather than opening up excel and doing a find search each time. Well, the easiest (maybe not fastest) way would be something like: rows_found = [] for row in rows: for element in row: try: if 'scrip' in element: rows_found.append(row) break # Once we find an element, no need to check others except TypeError: pass #can't iterate over int's such That should give you the correct results. Alternatively, if you have something against try/except you could do for row in rows if 'scrip' in str(row): #str(row) turns the list into the same thing you get when you print(row) rows_found.append(row) I'm not sure which one is faster, though I presume the first one would be. I don't know if either of these are the best options (they probably aren't), but they should work, and for 3500 it will probably loop faster than opening up excel. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to find a substring within a list of items
Richard Querin rfque...@gmail.com wrote I have an object that contains about 3500 list items, each list containing various data, some strings and some floats, like so: ['D', 123.4,'This is a project description', 'type', 52.1,'title'] What is the easiest way to search this list for a given string? Is the format constant? In other words are items 2 and 5 always numbers and therefore can be ignored? If so it might be something as simple as: result = [] for item in data: for index in [0,2,3,5]: if 'scrip' in item[index].lower(): result.append( item) break And you can optimise that as much as you like... :-) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to find a substring within a list of items
On Thu, Jan 13, 2011 at 2:27 PM, Wayne Werner waynejwer...@gmail.comwrote: I don't know if either of these are the best options (they probably aren't), but they should work, and for 3500 it will probably loop faster than opening up excel. HTH, Wayne Thanks Wayne. This would definitely be faster than getting Excel opened and doing it there. Given Alan's great suggestion in only stepping through string fields (this *does* have constant object formats) things should be even quicker. Now to give them a nice simple GUI to do it in. :) Thinking about wxPython (what I'm most used to), though it's been a while. Not sure if there are better options for something simple like this. Great help guys. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor