[Tutor] searching through a string list
Andrei Kulakov
ak@silmarill.org
Thu, 8 Aug 2002 21:42:15 -0400
On Thu, Aug 08, 2002 at 06:22:31PM -0700, Mathew P. wrote:
> I have a huge list (imagine a list of pairs that is like, 12,000
> entries long). The pairs each consist of a number and a persons name,
> both in string form (a list of lists, each sublist containing the
> pair). I need to parse this list, which I can figure out how to do, but
> while parsing it, I need to be able to search for a persons name.
>
> This list will have the same names in it more than once, and what I am
> actually doing is parsing the list to find out how many times a persons
> name appears in the list. To complicate things, I need to be able to do
> a partial match. For instance, I need to be able to find out how many
> "anthony" 's appear in the list - so if I have an anthony brown, and
> anthony johnson, and an anthony williams, the program will count three
> anthonys.
> I
> I was sure that the string library would have search facilities that
> would do just what I wanted. I have not found exactly what I was
> looking for though. The closest thing I came to was the string.find()
> method. Will string.find() (inside of a while or for loop) do partial
> matches for me like this? If so, can someone give me an example of how
> to use the find method, or point me to a URL? The python library docs
> have no example code that I was able to find, to illustrate how to use
> find.
>
> Thanks ahead of time,
>
> Mathew
This sounds like it ought to be a dictionary - {"person name":
[number1, number2], ...}
Then if you wanted to find all anthonies, you could do:
results = []
for key in mydict.keys():
if key.find("anthony") != -1:
results.append(key)
>
>
> __________________________________________________
> Do You Yahoo!?
> HotJobs - Search Thousands of New Jobs
> http://www.hotjobs.com
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Cymbaline: intelligent learning mp3 player: python, linux, console.
get it at: http://silmarill.org/cymbaline.htm