簡體   English   中英

以下for循環如何工作?

[英]How does the following for loop work?

def escape_html(s):
    for (i, o) in (("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;")):
        s = s.replace(i , o)
    return s

我以前沒見過這樣的東西。

for循環的第一行是什么意思?

一般來說,循環是做什么的,它是如何做到的?

注意:s是一個字符串

請嘗試解釋完整的迭代過程。

用英語:

對於以下值對列表中的每對值,請在循環中執行這些操作。 在這種情況下,(i,o)只是意味着“將對中的值分配給名為i&o的變量”。

在第一次迭代期間, i是“&”而o是“&amp;”

每次循環時,它用o的替換替換i出現,因此源文本中的任何“&”變為“&amp;”,“>”變為“&gt”等。

這是非常直接的python。

for循環從可迭代中解包單個項目。 所以,舉個例子你可以這樣做:

>>> c = [('a', 'b', 'c'), ('d', 'e', 'f')]
>>> for i, j, k in c:
...     print i, j, k
... 
a b c
d e f

在你的情況下(i, o)正在填充元組元組的值。 然后將i實例替換為o的值。 此函數正在用表示每個字符的實體替換html特殊字符。

>>> s = 'foo & bar'
>>> s = s.replace('&', '&amp;')
>>> s
'foo &amp; bar'

這個函數等效地做:

def escape_html(s):
    s = s.replace("&","&amp;")
    s = s.replace(">", "&gt;")
    s = s.replace("<", "&lt")
    s = s.replace('"', "&quot;")
    return s

代替使用正確的調試器,嘗試添加一些打印語句以查看發生了什么:

def escape_html(s):
    print "ORIGINAL STRING: %s" % (s)
    for (i, o) in (("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;")):
        print "\t(i, o) = ('%s', '%s')" % (i, o)
        s = s.replace(i , o)
        print "\ts = %s" % (s, )
        print
    return s

mystring = """<h3>This is a test</h3><script>alert("I hacked you!");</script>"""
print escape_html(mystring)

OUTPUT

ORIGINAL STRING: <h3>This is a test</h3><script>alert("I hacked you!");</script>
    (i, o) = ('&', '&amp;')
    s = <h3>This is a test</h3><script>alert("I hacked you!");</script>

    (i, o) = ('>', '&gt;')
    s = <h3&gt;This is a test</h3&gt;<script&gt;alert("I hacked you!");</script&gt;

    (i, o) = ('<', '&lt')
    s = &lth3&gt;This is a test&lt/h3&gt;&ltscript&gt;alert("I hacked you!");&lt/script&gt;

    (i, o) = ('"', '&quot;')
    s = &lth3&gt;This is a test&lt/h3&gt;&ltscript&gt;alert(&quot;I hacked you!&quot;);&lt/script&gt;

&lth3&gt;This is a test&lt/h3&gt;&ltscript&gt;alert(&quot;I hacked you!&quot;);&lt/script&gt;

for每對物品的io in對序列(("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;"))replace S的每個實例io在串s

for (i, o) in (("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;")):

我和o是你的循環變量。 & > < "是要替換的字符, &amp; &gt; &lt; &quot;是要替換它們的字符。

在循環的第一次迭代中, i = &o = &amp; 在第二次迭代中, i = >o = &gt; 等等。

你迭代的東西是一個元組元組(在這種情況下是對)。

所以對於循環的每次迭代,我得到第一個東西,而o得到第二個。 EG,在第一次迭代中,我得到&和o得到&。

所以它只是繼續創建新的字符串,用i代替。

將元組視為tupl =(("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;"))使它更簡單。

所以tupl的項目是("&","&amp;")(">", "&gt;")等等

所以for循環變成:

  for (i,o) in tupl:

它的作用是從tupl逐個獲取項目嘗試做類似的事情:

(i,o)=("&","&amp;") ,或簡單地說i,o=("&","&amp;") ,它將'&'分配給i&amp; 到第一次迭代中的o>i&gt; o在第二次迭代中,依此類推。

(("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;"))是元組中的元組。

讓我們把它簡化為更簡單的術語。

for (x, y) in ( ('a', 'b'), ('c', 'd') ):
    print x, y   

這打印出每個元組的內容......

a, b
c, d

或許這可以解決問題。

(("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;"))是一個包含4個元素的元組在里面。

索引0處的元素是元組("&","&amp;")

當你說a, b = 0, 1 ,python將它與(a, b) = (0, 1) ,其中變量被賦予相應的值。 也就是說, a取值0b取值1

你的for循環有效地遍歷大元組,里面有4個元素。 由於每個元素都是2元組,因此您可以將它們各自的值分配給兩個變量io

(("&","&amp;"),(">", "&gt;"),("<", "&lt"),('"', "&quot;"))是一個4元組每個元素都是一個2元組(例如, ("&","&amp;") )。元組是一個固定長度的序列。你可以在這里閱讀更多信息: http://anh.cs。 luc.edu/python/hands-on/3.1/handsonHtml/loopsandtuples.html

第一行只是序列上的for循環。 左側(在'in'之前)利用python解包。 它采用元組的兩個值並將它們分配,一個進入i ,另一個進入o

通常,對於每個元組,for循環用第二個元素替換元組的第一個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM