Reputation: 1419
I'm trying to write a function that takes two string arguments and returns the number of times a character from the first string occurs in the second string.
I am a complete beginner to python and am stumped. If anyone could point me in the right direction that would be great. I've been given this to start with:
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2
occurrences(string, string) -> int
"""
#Your code goes here
As you can see, 2 strings are needed. I thought that string 1 and string 2 would be sufficient but I have no idea how to to define them.
I have started with this so far and I'm not even having any success.
for c in "string":
print c
if c == char c in "string2":
count += 1
I'm just throwing in random variables because how am I suppose to find char(A-Z) in a string that I dont even know?
EDIT: some of the tips you guys have told me i havn't learned yet. For this question i should be using:
in
Some hints were given to me also:
Hint 1: You might find in
useful for testing if one string is in another string.
Hint 2: Look at each character in the second argument and see if it is in the first argument.
Upvotes: 2
Views: 2766
Reputation: 29302
Since this sounds like homework, I'm just going to give you some pointers about what you'll need to do:
Find out which charachters are in the text1
string. The point is that you wont interate over the same charachter twice.
set()
can help you with that:
>>> set('fooled')
set(['d', 'e', 'l', 'o', 'f'])
Try to play a bit with them.
Iterate over the set of (different) charachters from text1
. You can use .count()
:
>>> 'hello world'.count('e')
1
>>> 'hello world'.count('o')
2
This can count how many times a charachter occures in a string, you'll need to sum all those values and return that sum.
Note: There are many ways for doing what you're asking this is only one of them (and not the best performant one). If you look around searching for "count string occurences" or something like, that you can find other interesting solutions :)
Another approach could be to to start from text2
:
text2
in text1
, than increment by one your sum.Update: Have you tried a bit to play with them?
Check the difference between:
>>> word = 'fooled'
>>> for c in word:
... print(c)
and:
>>> word = 'fooled'
>>> for c in set(word):
... print(c)
It shouldn't be hard to call inside the for-loop text2.count(c)
.
If this still "doens't make much sense", than I'd suggest to read a good Python Tutorial and come back later.
Upvotes: 1
Reputation: 61478
Let's start here and have a bit of a discussion:
As you can see, 2 strings are needed. I thought that string 1 and string 2 would be sufficient but I have no idea how to to define them.
They are provided for you: they are called text1
and text2
. They come from the code that calls the functions. Were functions explained to you at some point? In your own words, how does a function work? What will the code look like that calls occurrences
?
(Hint for the last part: there is an example given on your assignment sheet.)
Next:
if c == char c in "string2":
What do you expect this to mean? In particular, what do you expect char
to mean? (Have you studied programming languages other than Python before?)
Upvotes: 4
Reputation: 1169
for python 2.7 and higher:
[ Solution in previous version of question ]
Upvotes: -1
Reputation: 49816
So, you know how to iterate over the characters in a string: good.
What you are not doing is anything that could count them. You need a set of counters to keep track of each character in the string. One way to achieve that is with a dict
, or collections.defaultdict
, or another class found in collections
.
Use the docs, Luke.
Bonus tip: It is possible to do this (readably) in O(m+n) time, in a single line, using appropriate datastructures and a list comprehension (google it!).
Upvotes: 0
Reputation: 107588
You have to put your code in the function and use the arguments the function has. This should help you figure out how it works:
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2
occurrences(string, string) -> int
"""
# loop through the string in the variable `text1`
for c in text1:
print c
# see if its in `text2`
if c in text2:
pass # you do the rest ;)
# calls the function with 'fooled' -> text1 and 'hello world' -> text2
print occurrences('fooled', 'hello world')
Upvotes: 1