Member Avatar for soUPERMan

I need help to compare letters between two words, the first word letters should be used to compare the letters in the second word...
I have an idea that i could use the for loop but im not quite sure how. Please help!

If you want compare words...

>>> a = 'I love DaniWEb forum and I`m Python user'
>>> b = 'I want to help on my friend who also is Python user with comparing two sentences'
>>> first = a.split(' ')
>>> second = b.split(' ')
>>> same = 0
>>> for word in first:
	for word2 in second:
		if word == word2:
			same += 1		
>>> same
3
>>>
Member Avatar for soUPERMan

If you want compare words...

>>> a = 'I love DaniWEb forum and I`m Python user'
>>> b = 'I want to help on my friend who also is Python user with comparing two sentences'
>>> first = a.split(' ')
>>> second = b.split(' ')
>>> same = 0
>>> for word in first:
	for word2 in second:
		if word == word2:
			same += 1		
>>> same
3
>>>

Im assuming that this code outputs how many times the similar character appears. What i want is for the first word to check and report the index of the common letter position on the first word and the second word. Thanks

Hint ...

word = "python"

for letter in word:
    index = word.find(letter)
    print( "letter %s is at index %s" % (letter, index) )
Member Avatar for soUPERMan

I've tried something, converted the words into a list of letters, the problem is to compare them and then printing the locations... :D
Here's the code:

def common_letter():
        word1 = raw_input("Enter word1: ")
        word2 = raw_input("Enter word2: ")
        li1 = []
        li2 = []
        for letter1 in word1:
                li1 += letter1
        for letter2 in word2:
                li2 += letter2
        print li1
        print li2

common_letter()
def common_letter():
        #word1 = raw_input("Enter word1: ")
        #word2 = raw_input("Enter word2: ")
        word1 = 'This is my car'
        word2 = 'My car is fine'        
        
        li1 = []
        li2 = []
        for letter1 in word1:
                li1.append(letter1) #This is better
        for letter2 in word2:
                li2 += letter2
        print li1  #Now you have two list with the word
        print li2  #And nothing is compared 
        
        diff_list = [item for item in li1 if not item in li2]
        #Can write it as a loop if this is hard to understand
        print diff_list 
       
common_letter()
Member Avatar for soUPERMan
def common_letter():
        #word1 = raw_input("Enter word1: ")
        #word2 = raw_input("Enter word2: ")
        word1 = 'This is my car'
        word2 = 'My car is fine'        
        
        li1 = []
        li2 = []
        for letter1 in word1:
                li1.append(letter1) #This is better
        for letter2 in word2:
                li2 += letter2
        print li1  #Now you have two list with the word
        print li2  #And nothing is compared 
        
        diff_list = [item for item in li1 if not item in li2]
        #Can write it as a loop if this is hard to understand
        print diff_list 
       
common_letter()

sorry, i do find it hard to understand, can u tell me what that means? or better can u write it in a loop.

word1 = 'This is my car'
word2 = 'My car is fine' 

diff_list = []
for item in word1:
    if not item in word2:
        diff_list.append(item)

print diff_list

And for index that i forget we can use vega code with a litte twist.

word1 = 'This is my car'
word2 = 'My car is fine' 

diff_list = []
for item in word1:
    if not item in word2:
        diff_list.append(item)

print diff_list

for letter in diff_list:
    index = str(diff_list).find(letter)    
    print( "letter %s is at index %s" % (letter, index))

'''Out-->
['T', 'h', 'm']
letter T is at index 2
letter h is at index 7
letter m is at index 12
'''
Member Avatar for soUPERMan
word1 = 'This is my car'
word2 = 'My car is fine' 

diff_list = []
for item in word1:
    if not item in word2:
        diff_list.append(item)

print diff_list

And for index that i forget we can use vega code with a litte twist.

word1 = 'This is my car'
word2 = 'My car is fine' 

diff_list = []
for item in word1:
    if not item in word2:
        diff_list.append(item)

print diff_list

for letter in diff_list:
    index = str(diff_list).find(letter)    
    print( "letter %s is at index %s" % (letter, index))

'''Out-->
['T', 'h', 'm']
letter T is at index 2
letter h is at index 7
letter m is at index 12
'''

So after a good shower. And thoughts of coffee...i managed to come up with this. :D

def common_letter():
        word1 = raw_input("Enter 1st word: ")
        word2 = raw_input("Enter 2nd word: ")
        common = ""
        response = ""
        for letter1 in word1:
                if letter1 in word2:
                        common += letter1 
        if common == "":
                print "No matching letter!"
        else:
                print "\"" + str(common[0]) + "\" is at position " + str(word1.find(common[0]) + 1) + " in " + word1
                print "\"" + str(common[0]) + "\" is at position " + str(word2.find(common[0]) + 1) + " in " + word2
        
common_letter()

Just what i wanted it to do. :)

Thanks everyone for the amazing help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.