first = "Patient has cancer"
second = "bhagwat has cancer"
def _transpositions(first, second):
d = [(f, s) for f, s in zip(first, second) if not f == s]
print(d)
_transpositions(first,second)
其输出为:
[('a', 'h'), ('t', 'a'), ('n', 'a'), (' ', 't'), ('h', ' '), ('a', 'n')]
请解释一下。你知道吗
您引用的代码段是list comprehension。等于代码:
它为
zip(first, second)
中的每个f, s
返回一个元组数组(f, s)
,其中f
不等于s
。你知道吗相关问题 更多 >
编程相关推荐