这个语法是什么意思[f(s)for f,s in zip(first,second)if if f == s] in python

时间:2017-07-17 14:30:48

标签: python

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')]

请解释。

1 个答案:

答案 0 :(得分:0)

您引用的代码是list comprehension。它等于代码:

d = []
for f, s in zip(first, second):
    if not f == s:
        d.append((f, s))

它为(f, s) f, szip(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>"; // 数组。