Python 两个字符串中出现的共同字符
我想使用for循环来计算一个字符串中的字符在另一个字符串中出现的次数 e、 g.如果string1='python'和string2='boa constractor',那么它应该计算到6(2t's,3o's,1n)Python 两个字符串中出现的共同字符,python,for-loop,Python,For Loop,我想使用for循环来计算一个字符串中的字符在另一个字符串中出现的次数 e、 g.如果string1='python'和string2='boa constractor',那么它应该计算到6(2t's,3o's,1n) 有人知道怎么做吗?使用dict comprehension{ch:string2.count(ch)for ch in string1如果ch in string2} 我忘了你需要一个循环和所有字母的总和 count = 0 for ch in string1: if c
有人知道怎么做吗?使用dict comprehension
{ch:string2.count(ch)for ch in string1如果ch in string2}
我忘了你需要一个循环和所有字母的总和
count = 0
for ch in string1:
if ch in string2:
count += string2.count(ch)
非常简单:
count = 0
for letter in set(string1):
count += string2.count(letter)
print(count)
他有一只虫子。你应该使用set(string1)。对不起,论坛上新出现了…和Python。是的,这是一个家庭作业问题。Moreso教自己如何通过做随机问题来使用程序。