我需要编写一个函数来计算一个字符串中的字符在另一字符串中出现的次数。
到目前为止,这就是我所拥有的,
def occurrences(text1, text2):
"""Return the number of times characters from text1 occur in text2
occurrences(string, string) -> int
"""
count = 0
#setup = set('text1')
for text1 in text2:
if text1 == text2:
count += 1
return count
我不明白如何比较字符串,因为它们将是随机生成的。我知道我需要查看第二个参数中的每个字符,并查看它是否在第一个参数中。
我知道我需要查看第二个参数中的每个字符,并查看它是否在第一个参数中。
这正是您需要编写的内容:
for character in text2:
if character in text1:
count += 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句