tags:

views:

64

answers:

6

If i have List PhoneDirectory Eg:

['John:009878788677' , 'Jefrey:67654654645' , 'Maria:8787677766']

Which is the function that can be use to compare the Presence of Substring (Eg: Joh) in each entry in the List .

I have tried using

if(PhoneDirectory.find(Joh) != -1)

but it doesnt work

kindly Help..

A: 

You can do a split on ":" and look for occurrences of what you are looking for in the first element of the resulting array.

dir = ['John:009878788677' , 'Jefrey:67654654645' , 'Maria:8787677766'];
for a in dir:
    values = a.split(":")
    if values[0] == "John":
        print("John's number is %s" % (values[1]))
Pablo Santa Cruz
A bit overly complicated, there's really no need to split each entry.
Justin Ardini
True. This is the only way I know how to do that in Python! :-) Loved the "any" answer.
Pablo Santa Cruz
Thanks That was great.
Nilesh Nar
+7  A: 

If you want to check each entry separately:

for entry in PhoneDirectory:
    if 'John' in entry: ...

If you just want to know if any entry satisfies the condition and don't care which one:

if any('John' in entry for entry in PhoneDirectory):
    ...

Note that any will do no "wasted" work -- it will return True as soon as it finds any one entry meeting the condition (if no entries meet the condition, it does have to check every single one of them to confirm that, of course, and then returns False).

Alex Martelli
+1 for `any`. Very cool.
Justin Ardini
As you are close to development of Python, do you know why any does not return the first "True" value (say "Johnny") but only less useful True (even the interpreter must know the exact answer). That is: to work like or in Python or "useful result" principle in Lisp? BTW maybe it should be:if any(entry.startswith'John:' in entry for entry in PhoneDirectory)
Tony Veijalainen
@Tony, since `any` has to return `False` (e.g. for an empty list, can't just "return some false value from the sequence") when it says "no", for symmetry reason it was judged better to always make it return a boolean. Re your BTW, perhaps, but (if so) then only after fixing the syntax error in your suggestion (put parentheses around `'John:'`;-).
Alex Martelli
I have not your godly powers to change it any more. Should always run the code before sending out ;-)
Tony Veijalainen
I found the answer working for me.Thanks a lot.
Nilesh Nar
@user, you're welcome!
Alex Martelli
A: 

You can use a list comprehension, like [p for p in PhoneDirectory if p.find('Joh') != -1]. This will give you a list of all matching items.

Bob
+1  A: 

Since no one has recommended this yet, I would do:

all_johns = [p for p in PhoneDirectory if 'Joh' in p]
Justin Ardini
A: 

If performance counts for this task use some SuffixTree implementation. Or just make any DBMS engine do the indexing job.

Odomontois
A: 
if any(entry.startswith('John:') in entry for entry in PhoneDirectory)

But I would prepare something with two elements as you list of strings is not well suited to task:

PhoneList = ['John:009878788677' , 'Jefrey:67654654645' , 'Maria:8787677766']

numbers = { a:b 
            for item in PhoneList
            for a,_,b in (item.partition(':'),)
            }

print numbers
print "%s's number is %s." % ( 'John', numbers['John'] )
Tony Veijalainen