• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Welcome to 陳俊峰's ---BeetleHeaded Man Blog !

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              58 隨筆 :: 32 文章 :: 18 評論 :: 0 Trackbacks
            Python Reading Notes (2006-4-13) ?

            Note One:about list (wow! so powerful)

            Assignment to slices is also possible, and this can even change the size of the list,but you can not do that in the string type:
            >>> # Replace some items:
            ... a[0:2] = [1, 12]
            >>> a
            [1, 12, 123, 1234]
            >>> # Remove some:
            ... a[0:2] = []
            >>> a
            [123, 1234]
            >>> # Insert some:
            ... a[1:1] = ['bletch', 'xyzzy']
            >>> a
            [123, 'bletch', 'xyzzy', 1234]
            >>> a[:0] = a???? # Insert (a copy of) itself at the beginning
            >>> a
            [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]

            It is possible to nest lists (create lists containing other lists), for example:
            >>> q = [2, 3]
            >>> p = [1, q, 4]
            >>> len(p)
            3
            >>> p[1]
            [2, 3]
            >>> p[1][0]
            2
            >>> p[1].append('xtra')????
            >>> p
            [1, [2, 3, 'xtra'], 4]
            >>> q
            [2, 3, 'xtra']


            Note Two : about for statement

            It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient:

            valid statements: it will work normally
            >>> for x in a[:]: # make a slice copy of the entire list
            ...?????? if len(x) > 6:?
            ...???????????a.insert(0, x)
            ...
            >>> a
            ['defenestrate', 'cat', 'window', 'defenestrate']

            invalid statement: you prompt will die
            >>> for x in a :? # make a slice copy of the entire list
            ...??? ???if len(x) > 6:
            ...????????? a.insert(0, x)
            ...

            Note that: in the first statement?the first row ,the?for statement?ues 'a[:] ' ,it means that?to make a slice of?it;but in the second statement,the for statement ues 'a' directly ,so cause a?bad?result?

            Note Three: Compare for efficiency

            str_test = []
            #?method one :
            str_test.append('attach')
            #method two:
            str_test = str_test?+ ['attach']

            The?Two?method above has the?same function ,but the more efficient one is the first one
            ?
            Note Four :about Defining Functions?
            Form 1:Default Argument Values
            The default value is evaluated only once.
            def f(a, L=[]):
            ??? L.append(a)
            ??? return L

            print f(1)
            print f(2)
            print f(3)

            This will print

            [1]
            [1, 2]
            [1, 2, 3]
            

            If you don't want the default to be shared between subsequent calls, you can write the function like this instead:

            def f(a, L=None):
                if L is None:
                    L = []
                L.append(a)
                return L

            Form 2 : Keyword Arguments

            When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except
            for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in
            the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must
            occur before **name) For example, if we define a function like this:

            def cheeseshop(kind, *arguments, **keywords):
                print "-- Do you have any", kind, '?'
                print "-- I'm sorry, we're all out of", kind
                for arg in arguments: print arg
                print '-'*40
            keys = keywords.keys() keys.sort() for kw in keys: print kw, ':', keywords[kw]

            It could be called like this:

            cheeseshop('Limburger', "It's very runny, sir.",
                       "It's really very, VERY runny, sir.",
                       client='John Cleese',
                       shopkeeper='Michael Palin',
                       sketch='Cheese Shop Sketch')

            and of course it would print:

            -- Do you have any Limburger ?
            -- I'm sorry, we're all out of Limburger
            It's very runny, sir.
            It's really very, VERY runny, sir.
            ----------------------------------------
            client : John Cleese
            shopkeeper : Michael Palin
            sketch : Cheese Shop Sketch

            Note that the sort() method of the list of keyword argument names is called before printing the contents of the keywords
            dictionary; if this is not done, the order in which the arguments are printed is undefined.

            posted on 2006-04-13 10:50 Jeff-Chen 閱讀(165) 評論(0)  編輯 收藏 引用 所屬分類: Python
            亚洲国产精品久久久久婷婷老年| 国产精品久久久久久| 久久亚洲国产成人精品无码区| 日批日出水久久亚洲精品tv| 久久精品国产乱子伦| 亚洲乱亚洲乱淫久久| 久久亚洲AV无码精品色午夜麻豆| 91久久精品91久久性色| 久久精品夜色噜噜亚洲A∨| 亚洲国产精品无码久久久不卡| 国产精品美女久久久免费| 久久无码中文字幕东京热| 91精品国产高清久久久久久国产嫩草| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 99久久精品国产毛片| 伊人久久大香线蕉综合影院首页| 伊人热人久久中文字幕| 久久久久99精品成人片欧美| 亚洲精品午夜国产va久久| 99久久亚洲综合精品成人| 久久这里只有精品18| 人妻无码精品久久亚瑟影视| 久久精品国产福利国产琪琪| 国产精品成人精品久久久| 精品九九久久国内精品| 国产产无码乱码精品久久鸭| 久久久无码人妻精品无码| 国产精品99久久久精品无码| 亚洲国产香蕉人人爽成AV片久久| 久久99精品久久久久久齐齐| 久久av高潮av无码av喷吹| 26uuu久久五月天| 成人亚洲欧美久久久久| 日韩精品国产自在久久现线拍| 久久噜噜电影你懂的| 国内精品久久九九国产精品| 久久国产免费观看精品| 国产一区二区精品久久凹凸| 欧美久久一级内射wwwwww.| 蜜臀久久99精品久久久久久| 亚洲七七久久精品中文国产 |