user1210588
user1210588

Reputation: 67

Python Function to return a list of common letters in first and last names

Question: DO NOT USE SETS IN YOUR FUNCTION: Uses lists to return a list of the common letters in the first and last names (the intersection) Prompt user for first and last name and call the function with the first and last names as arguments and print the returned list.

I can't figure out why my program is just printing "No matches" even if there are letter matches. Anything helps! Thanks a bunch!

Code so far:

import string

def getCommonLetters(text1, text2):
""" Take two strings and return a list of letters common to
    both strings."""
    text1List = text1.split()
    text2List = text2.split()
    for i in range(0, len(text1List)):
        text1List[i] = getCleanText(text1List[i])
    for i in range(0, len(text2List)):
        text2List[i] = getCleanText(text2List[i])

    outList = []
    for letter in text1List:
        if letter in text2List and letter not in outList:
           outList.append(letter)
    return outList

def getCleanText(text):
"""Return letter in lower case stripped of whitespace and
punctuation characters"""
    text = text.lower()

    badCharacters = string.whitespace + string.punctuation
    for character in badCharacters:
        text = text.replace(character, "")
    return text

userText1  = raw_input("Enter your first name: ")
userText2  = raw_input("Enter your last name: ")
result     = getCommonLetters(userText1, userText2)
numMatches = len(result)
if numMatches == 0:
    print "No matches."
else:
    print "Number of matches:", numMatches

for letter in result:
    print letter

Upvotes: 1

Views: 6225

Answers (3)

user688635
user688635

Reputation:

Prior to set() being the common idiom for duplicate removal in Python 2.5, you could use the conversion of a list to a dictionary to remove duplicates.

Here is an example:

def CommonLetters(s1, s2):
    d={}
    for l in s1:
        if l in s2 and l.isalpha():
            d[l]=d.get(l,0)+1
    return d                             

print CommonLetters('matteo', 'dom de tommaso')   

This prints the count of the common letters like so:

{'a': 1, 'e': 1, 'm': 1, 't': 2, 'o': 1} 

If you want to have a list of those common letters, just use the keys() method of the dictionary:

print CommonLetters('matteo', 'dom de tommaso').keys()

Which prints just the keys:

['a', 'e', 'm', 't', 'o']

If you want upper and lower case letters to match, add the logic to this line:

if l in s2 and l.isalpha():

Upvotes: 1

Vincent Savard
Vincent Savard

Reputation: 35947

for letter in text1List:

Here's your problem. text1List is a list, not a string. You iterate on a list of strings (['Bobby', 'Tables'] for instance) and you check if 'Bobby' is in the list text2List.

You want to iterate on every character of your string text1 and check if it is present in the string text2.

There's a few non-pythonic idioms in your code, but you'll learn that in time.

Follow-up: What happens if I type my first name in lowercase and my last name in uppercase? Will your code find any match?

Upvotes: 3

the wolf
the wolf

Reputation: 35572

Try this:

def CommonLetters(s1, s2):
    l1=list(''.join(s1.split()))
    l2=list(''.join(s2.split()))
    return [x for x in l1 if x in l2]

print CommonLetters('Tom','Dom de Tommaso')    

Output:

>>> ['T', 'o', 'm']

Upvotes: 3

Related Questions