Reputation: 181
I'm trying to find the number of characters in the first string that are also found in the second string. This is what I have so far:
def main():
text1 = raw_input("Enter word 1: ")
text2 = raw_input("Enter word 2: ")
print("The number of characters that occur in both", text1, "and", text2, "is", count)
def count(text1, text2):
char = 0
for i in text1:
for j in text2:
if i == j:
char += 1
return char
main()
I'm having trouble at def count(). I'm not sure how I can count how many letters occur in text1 and text2 that won't be repeated. Any help is appreciated!
Upvotes: 1
Views: 1457
Reputation: 180391
Use set.intersection:
(set(text1).intersection(item2))
In [22]: len(set("bana").intersection("banana"))
Out[22]: 3
intersection() will accept any iterable as an argument.
def count(text1, text2):
return len(set(text1).intersection(text2))
In [24]: count("foo","foobar")
Out[24]: 2
If you want duplicates:
def count(text1, text2):
s = set(text2)
return len([x for x in text1 if x in s])
In [29]: count("foo","foobar")
Out[29]: 3
In [30]: count("bana","banana")
Out[30]: 4
Upvotes: 3
Reputation: 6587
I am not sure if this is what you are looking for, but if you need to add up the total number of letters in text1 and text2, and print the result, that's very easy to do. Please see below:
text1 = raw_input("Enter word 1: ")
text2 = raw_input("Enter word 2: ")
x = len(text1) + len(text2)
print "The number of characters that occur in both text1 and text2 is " + str(x)
Upvotes: 0
Reputation: 239453
The main problem is, you are not invoking the count
function, but printing the count
function object. It should have been
print("The number of....", text2, "is", count(text1, text2))
Also, you need to increment the counter, if the letter is found in the second string and then you can skip to the next character in the first string. So, you can break out of the loop, like this
for i in text1:
for j in text2:
if i == j:
char += 1
break
Since we consider only the first occurrence of i
in text2
, we can check if it exists in text2
, like this
for i in text1:
if i in text2:
char += 1
But remember, it will not consider the duplicates in the original string. For example, aaa
and a
will result in count 3
.
Upvotes: 2
Reputation: 6935
This would be a great time to use a set
, which automatically eliminates duplicates and allows instant lookup.
def count(text1, text2):
numchars = 0
text1 = set(list(text1))
text2 = set(list(text2))
for char in text1:
if char in text2:
numchars += 1
return numchars
By converting a string to a list, then to a set, you'll eliminate all duplicate characters in each string (only in the function, though, the strings are fine). You can then check each character in the first set to see if it's in the second set, which is a fixed-time check.
Another note: if you don't want to count uppercase A
and lowercase a
as two different characters, use text1 = set(list(text1.lower()))
instead.
Additionally, you are not invoking the function correctly. You should use count(text1,text2)
instead of just count
. Though if you're going to print it out, you need to use str(count(text1,text2))
, because the count()
function returns an integer and you can't just concatenate that with a string.
Upvotes: 0
Reputation: 629
You can do this by intersecting the strings (getting the unique characters in 2 strings) and taking the length of the result. Something like this:
def strIntersection(s1, s2):
out = ""
for c in s1:
if c in s2 and not c in out:
out += c
return out
>>> strIntersection('asdfasdfasfd' , 'qazwsxedc')
'asd'
and
len(strIntersection('asdfasdfasfd' , 'qazwsxedc'))
3
could be your count function.
(Also, you need to call count(text1, text2) and not just conut in your print statement, otherwise it won't call the function)
Upvotes: 0