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')]
请解释。
答案 0 :(得分:0)
您引用的代码是list comprehension。它等于代码:
d = []
for f, s in zip(first, second):
if not f == s:
d.append((f, s))
它为(f, s)
f, s
中zip(first, second)
不等于f
的每个s
返回一个元组$html_segment = "your html part with multiple script tags";
$insert_before ="<script to put BEFORE><br/>";
$insert_after = "<br/><script to put AFTER>";
$avoid_tag = "defer";
$search_tag ="jQuery";
//
$temp = explode("<script", $html_segment);
$result = "";
$len = count($temp);
for ($i=0; $i<$len; $i++) {
$part = $temp[$i];
// check if this part contains 'jQuery'
// and does NOT contain 'defer'
// if not -> do something
if (strpos($part, $search_tag) !== false &&
strpos($part, $avoid_tag) === false) {
// change
$part = $insert_before."<script".$part;
$part = str_replace("</script>", "</script>$insert_after", $part);
} else if ($i >0) {
// put back the original
$part = "<script".$part;
}
$result.=$part;
}
//END: $result now has the new HTML
// proof:
echo "<textarea>$result</textarea>";
//
数组。