1 python
python中是否存在任何内置函数,而不是可以返回两个字符串中的数学字符数,例如:
INPUT:
TICK TOCK
CAT DOG
APPLE APPLES
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
3
0
5
Run Code Online (Sandbox Code Playgroud)
单词"TICK"和"TOCK"的得分为3,因为三个字符(T,C,K)是相同的.同样,"CAT"和"DOG"得分为0,因为没有字母匹配.
我是python中的新bie所以请帮我举例.
这是使用列表推导的版本:
[x == y for (x, y) in zip("TICK", "TOCK")].count(True)
Run Code Online (Sandbox Code Playgroud)
或者,更短(使用operator):
import operator
map(operator.eq, "TICK", "TOCK").count(True)
Run Code Online (Sandbox Code Playgroud)
根据@Kabie,<expr>.count(True)可以sum(<expr>)在两个版本中替换.