<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>

    習題 39: 列表的操作?

    你已經學過了列表。在你學習“while 循環”的時候,你對列表進行過“追加(append)”操作,而且將列表的內容打印了出來。另外你應該還在加分習題里研究過 Python 文檔,看了列表支持的其他操作。這已經是一段時間以前了,所以如果你不記得了的話,就回到本書的前面再復習一遍把。

    找到了嗎?還記得嗎?很好。那時候你對一個列表執行了 append 函數。不過,你也許還沒有真正明白發生的事情,所以我們再來看看我們可以對列表進行什么樣的操作。

    當你看到像 mystuff.append('hello') 這樣的代碼時,你事實上已經在 Python 內部激發了一個連鎖反應。以下是它的工作原理:

    1. Python 看到你用到了 mystuff ,于是就去找到這個變量。也許它需要倒著檢查看你有沒有在哪里用 = 創建過這個變量,或者檢查它是不是一個函數參數,或者看它是不是一個全局變量。不管哪種方式,它得先找到 mystuff 這個變量才行。
    2. 一旦它找到了 mystuff ,就輪到處理句點 . (period) 這個操作符,而且開始查看 mystuff 內部的一些變量了。由于 mystuff 是一個列表,Python 知道mystuff 支持一些函數。
    3. 接下來輪到了處理 append 。Python 會將 “append” 和 mystuff 支持的所有函數的名稱一一對比,如果確實其中有一個叫 append 的函數,那么 Python 就會去使用這個函數。
    4. 接下來 Python 看到了括號 ( (parenthesis) 并且意識到, “噢,原來這應該是一個函數”,到了這里,它就正常會調用這個函數了,不過這里的函數還要多一個參數才行。
    5. 這個額外的參數其實是…… mystuff! 我知道,很奇怪是不是?不過這就是 Python 的工作原理,所以還是記住這一點,就當它是正常的好了。真正發生的事情其實是 append(mystuff, 'hello') ,不過你看到的只是 mystuff.append('hello')

    大部分時候你不需要知道這些細節,不過如果你看到一個像這樣的 Python 錯誤信息的時候,上面的細節就對你有用了:

    $ python
    Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> class Thing(object):
    ...     def test(hi):
    ...             print "hi"
    ...
    >>> a = Thing()
    >>> a.test("hello")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: test() takes exactly 1 argument (2 given)
    >>>
    

    就是這個嗎?嗯,這個是我在 Python 命令行下展示給你的一點魔法。你還沒有見過class 不過后面很快就要碰到了。現在你看到 Python 說 test() takes exactly 1 argument (2 given) (test() 只可以接受1個參數,實際上給了兩個)。它意味著 python 把 a.test("hello") 改成了 test(a, "hello") ,而有人弄錯了,沒有為它添加 a 這個參數。

    一下子要消化這么多可能有點難度,不過我們將做幾個練習,讓你頭腦中有一個深刻的印象。下面的練習將字符串和列表混在一起,看看你能不能在里邊找出點樂子來:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    ten_things = "Apples Oranges Crows Telephone Light Sugar"
    
    print "Wait there's not 10 things in that list, let's fix that."
    
    stuff = ten_things.split(' ')
    more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"]
    
    while len(stuff) != 10:
        next_one = more_stuff.pop()
        print "Adding: ", next_one
        stuff.append(next_one)
        print "There's %d items now." % len(stuff)
    
    print "There we go: ", stuff
    
    print "Let's do some things with stuff."
    
    print stuff[1]
    print stuff[-1] # whoa! fancy
    print stuff.pop()
    print ' '.join(stuff) # what? cool!
    print '#'.join(stuff[3:5]) # super stellar!
    

    你應該看到的結果?

    $ python ex39.py
    
    Wait there's not 10 things in that list, let's fix that.
    Adding:  Boy
    There's 7 items now.
    Adding:  Girl
    There's 8 items now.
    Adding:  Banana
    There's 9 items now.
    Adding:  Corn
    There's 10 items now.
    There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar',
          'Boy', 'Girl', 'Banana', 'Corn']
    Let's do some things with stuff.
    Oranges
    Corn
    Corn
    Apples Oranges Crows Telephone Light Sugar Boy Girl Banana
    Telephone#Light
    

    加分習題?

    1. 將每一個被調用的函數以上述的方式翻譯成 Python 實際執行的動作。例如: ' '.join(things) 其實是 join(' ', things)
    2. 將這兩種方式翻譯為自然語言。例如, ' '.join(things) 可以翻譯成“用 ‘ ‘ 連接(join) things”,而 join(' ', things) 的意思是“為 ‘ ‘ 和 things 調用 join 函數”。這其實是同一件事情。
    3. 上網閱讀一些關于“面向對象編程(Object Oriented Programming)”的資料。暈了吧?嗯,我以前也是。別擔心。你將從這本書學到足夠用的關于面向對象編程的基礎知識,而以后你還可以慢慢學到更多。
    4. 查一下 Python中的 “class” 是什么東西。不要閱讀關于其他語言的 “class” 的用法,這會讓你更糊涂。
    5. dir(something)something 的 class 有什么關系?
    6. 如果你不知道我講的是些什么東西,別擔心。程序員為了顯得自己聰明,于是就發明了 Object Oriented Programming,簡稱為 OOP,然后他們就開始濫用這個東西了。如果你覺得這東西太難,你可以開始學一下 “函數編程(functional programming)”。

    Project Versions

    Table Of Contents

    Previous topic

    習題 38: 閱讀代碼

    Next topic

    習題 40: 字典, 可愛的字典

    This Page

    97av