Python - Find number of characters that are found in both string1 and string2 -
I am trying to find the number of characters in the first string which also comes in the second string. This is what I've done so far:
def main (): text1 = raw_input (enter "word1:") text2 = raw_input ("word 2:") print ( "Number of characters that are in", text1, "and", text2, "both" count "), the DRT calculation (text1, lesson 2): for text = 1, in text I: text2 for j In: if i == j: char + = 1 return rayon main ()
I'm having trouble on def count () I'm not sure how I can count how How many letters are repeated in text1 and text2 Any help is appreciated!
The main problem is, you do not calculate
function, but count
function object printing. It should have been
print ("number of ....", lesson 2, "Yes", count (text1, lesson 2))
In addition, you need to increase the counter, if the characters meet in the second string, and then you will see the next string in the next On letter Can. So, you can break out of the loop, like << p>
i for text1: for text in j: if i == j: char + = 1 break
Since we only consider the first occurrence of text2
in i
, we can check whether it exists in text2
, Such as
i for text1: if i in text2: char + = 1
but remember, it will not consider duplicates in the original string . For example, the result of aaa
and a
will be counted in 3
.
Comments
Post a Comment