[Tutor] searching through a string list
Gregor Lingl
glingl@aon.at
Fri, 09 Aug 2002 04:23:58 +0200
Mathew P. schrieb:
>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.
>
>
Could you use this approach:
>>> l = [[1, 'anthony curl'], [2, 'anthony baxter']]
>>> str(l)
"[[1, 'anthony curl'], [2, 'anthony baxter']]"
>>> str(l).count('anthony')
2
>>>
?
Gregor