<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            Previous Page

            Up One Level

            Next Page

            Python???

            Contents

            ????? 4. ??????????? ????? Python ??? ??? 6. ???


            ????



             
            5.
            ?????

            ??????????????????????????????????????????????

             
            5.1
            ????????

            ??????????????????????????????????????

            append(

            x)

            ???????????????????????? a[len(a):] = [x]??

            extend(

            L)

            ???????????????????????????????????? a[len(a):] = L ??

            insert(

            i, x)

            ????????????????????????????????????????????????????????? a.insert(0, x) ??????????????????? a.insert(len(a), x) ???? a.append(x)??

            remove(

            x)

            ???????????x??????????????????????????????????????

            pop(

            [i])

            ???????????????????????????????????????????a.pop() ??????????????????r?????????????????????i?????????????????????????????????????????????????????????Python ???????????????????????

            index(

            x)

            ???????????????x????????????????????????????????????

            count(

            x)

            ????x????????????????

            sort(

            )

            ???????????????????????

            reverse(

            )

            ??????????????

            ????????????????????????????

            >>> a = [66.6, 333, 333, 1, 1234.5]
            >>> print a.count(333), a.count(66.6), a.count('x')
            2 1 0
            >>> a.insert(2, -1)
            >>> a.append(333)
            >>> a
            [66.6, 333, -1, 333, 1, 1234.5, 333]
            >>> a.index(333)
            1
            >>> a.remove(333)
            >>> a
            [66.6, -1, 333, 1, 1234.5, 333]
            >>> a.reverse()
            >>> a
            [333, 1234.5, 1, 333, -1, 66.6]
            >>> a.sort()
            >>> a
            [-1, 1, 66.6, 333, 333, 1234.5]

             
            5.1.1
            ???????????????

            ?????????????????????????????????????????????????????????????????????????????????????????? append() ????????????????????????????????????? pop() ???????????????????????????????

            >>> stack = [3, 4, 5]
            >>> stack.append(6)
            >>> stack.append(7)
            >>> stack
            [3, 4, 5, 6, 7]
            >>> stack.pop()
            7
            >>> stack
            [3, 4, 5, 6]
            >>> stack.pop()
            6
            >>> stack.pop()
            5
            >>> stack
            [3, 4]

             
            5.1.2
            ????????????????

            ?????????????????????????????????????????????????????????????????????????? append()?????????????????????????0????????? pop() ???????????????????????????????

            >>> queue = ["Eric", "John", "Michael"]
            >>> queue.append("Terry")           # Terry arrives
            >>> queue.append("Graham")          # Graham arrives
            >>> queue.pop(0)
            'Eric'
            >>> queue.pop(0)
            'John'
            >>> queue
            ['Michael', 'Terry', 'Graham']

             
            5.1.3
            ????????????

            ??????????????????????????????????filter()?? map()?? ?? reduce()??

            filter(function, sequence)” ???????????sequence?????????????????????????function(item)??????true?????????????????????????????????????????3?????????????????

            >>> def f(x): return x % 2 != 0 and x % 3 != 0
            ...
            >>> filter(f, range(2, 25))
            [5, 7, 11, 13, 17, 19, 23]

            map(function, sequence)” ?????????????? function(item) ?????????????????????????????3????????????

            >>> def cube(x): return x*x*x
            ...
            >>> map(cube, range(1, 11))
            [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]

            ?????????????????????????????????????????????????????????????????????????????????????????????None????????????None?????????????????????????????????

            ?????????????????????“map(None, list1, list2)”????????????????????????????

            >>> seq = range(8)
            >>> def square(x): return x*x
            ...
            >>> map(None, seq, map(square, seq))
            [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)]

            "reduce(func, sequence)" ?????????????????????????????????????????????????????????????????????????????????????????????3??????1??10??????????

            >>> def add(x,y): return x+y
            ...
            >>> reduce(add, range(1, 11))
            55

            ????????????????????????????????????????????????????

            ?????????????????????????????????????????????????????????????????????????????????????????????????????????????

            >>> def sum(seq):
            ...     def add(x,y): return x+y
            ...     return reduce(add, seq, 0)
            ... 
            >>> sum(range(1, 11))
            55
            >>> sum([])
            0

            ?????????????????? sum()????????????????????????????2.3????????????? sum(sequence) ??????

            5.1.4 ????????

            ????????????????????????????????????? map()?? filter() ??? lambda?? ???????????????????????????????????????????????????????for?????????????????for??if????????????for??if???????????????????????????????????????????????????????

            >>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
            >>> [weapon.strip() for weapon in freshfruit]
            ['banana', 'loganberry', 'passion fruit']
            >>> vec = [2, 4, 6]
            >>> [3*x for x in vec]
            [6, 12, 18]
            >>> [3*x for x in vec if x > 3]
            [12, 18]
            >>> [3*x for x in vec if x < 2]
            []
            >>> [[x,x**2] for x in vec]
            [[2, 4], [4, 16], [6, 36]]
            >>> [x, x**2 for x in vec]      # error - parens required for tuples
              File "<stdin>", line 1, in ?
                [x, x**2 for x in vec]
                           ^
            SyntaxError: invalid syntax
            >>> [(x, x**2) for x in vec]
            [(2, 4), (4, 16), (6, 36)]
            >>> vec1 = [2, 4, 6]
            >>> vec2 = [4, 3, -9]
            >>> [x*y for x in vec1 for y in vec2]
            [8, 6, -18, 16, 12, -36, 24, 18, -54]
            >>> [x+y for x in vec1 for y in vec2]
            [6, 5, -7, 8, 7, -5, 10, 9, -3]
            >>> [vec1[i]*vec2[i] for i in range(len(vec1))]
            [8, 12, -54]

            ?????????????for?????????????????????????????????

            >>> x = 100                     # this gets overwritten
            >>> [x**3 for x in range(5)]
            [0, 1, 8, 27, 64]
            >>> x                           # the final value for range(5)
            4

             
            5.2 del
            ???

            ??????????????????????????????????del?????????????????????????????????????????????????????????????

            >>> a = [-1, 1, 66.6, 333, 333, 1234.5]
            >>> del a[0]
            >>> a
            [1, 66.6, 333, 333, 1234.5]
            >>> del a[2:4]
            >>> a
            [1, 66.6, 1234.5]

            del ??????????????????????

            >>> del a

            ?????????????????????????????????????????????????????????????del??????????

             
            5.3
            ??Tuples?? ??????Sequences ??

            ????????????????????????????????????????????????????????????????????????Python???????????????????????????????????????????????????????????????????

            ???????????????????????????

            >>> t = 12345, 54321, 'hello!'
            >>> t[0]
            12345
            >>> t
            (12345, 54321, 'hello!')
            >>> # Tuples may be nested:
            ... u = t, (1, 2, 3, 4, 5)
            >>> u
            ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))

            ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

            ??????????????ĥx, y??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

            ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????a?????????????

            >>> empty = ()
            >>> singleton = 'hello',    # <-- note trailing comma
            >>> len(empty)
            0
            >>> len(singleton)
            1
            >>> singleton
            ('hello',)

            ??? t = 12345, 54321, 'hello!' ?????????sequence packing????????????? 12345?? 54321 ?? 'hello!' ????????????????????????????

            >>> x, y, z = t

            ?????????????????????????????????????????????????????????????????????????multiple assignment

            ????????????????????????????

            ???????????????????????????????????????????????????????????

             
            5.4
            ???Dictionaries??

            ?????????????Python????????????????Dictionaries????????????????????“???????”??``associative memories''????“????????”??``associative arrays''????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? append() ?? extend() ????????????????????????????????????????

            ???????????????????????????????????? key:value pairs ?????????????????????????????????????????????????????????????{}????????????????????????????W???????????????????????????????

            ???????????????????????????????????????del???????????????????????????????????????????????????????????????????????????????????????????????

            ????keys() ????????????????????????????????????????????????????????????????????sort()??????????????? has_key() ?????????????????????????????

            ???????????????????????

            >>> tel = {'jack': 4098, 'sape': 4139}
            >>> tel['guido'] = 4127
            >>> tel
            {'sape': 4139, 'guido': 4127, 'jack': 4098}
            >>> tel['jack']
            4098
            >>> del tel['sape']
            >>> tel['irv'] = 4127
            >>> tel
            {'guido': 4127, 'irv': 4127, 'jack': 4098}
            >>> tel.keys()
            ['guido', 'irv', 'jack']
            >>> tel.has_key('guido')
            True

            ?????՛?????-??????????????????????????????-???????????????????????????????????????-??????

            >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
            {'sape': 4139, 'jack': 4098, 'guido': 4127}
            >>> dict([(x, x**2) for x in vec])     # use a list comprehension
            {2: 4, 4: 16, 6: 36}

             
            5.5
            ???????

            ??????????????????????????????? items() ???????????????

            >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
            >>> for k, v in knights.items():
            ...     print k, v
            ...
            gallahad the pure
            robin the brave

            ?????????????????????????????? enumerate() ???????????

             
            >>> for i, v in enumerate(['tic', 'tac', 'toe']):
            ...     print i, v
            ...
            0 tic
            1 tac
            2 toe

            ?????????????????????????? zip() ????????

            >>> questions = ['name', 'quest', 'favorite color']
            >>> answers = ['lancelot', 'the holy grail', 'blue']
            >>> for q, a in zip(questions, answers):
            ...     print 'What is your %s?  It is %s.' % (q, a)
            ...     
            What is your name?  It is lancelot.
            What is your quest?  It is the holy grail.
            What is your favorite color?  It is blue.

             
            5.6
            ????????????

            ????while??if???????????????????????????

            in??not??????????????????????????????????is??is not??????????????????? ???????????????????????????????????????????????????????????????????????

            ?????????????????? a < b == c ??????a??b??b????c??

            ??????????????????????and??or????????????????not??????^??????????????????????????????????????not???????????????or????????????????A and not B or C ???? (A and (not B)) or C??????????????????????????????

            ?????????and ??or ?????????????????????????????????????????????????????????????A??C????B????A and B and C ???????C???????????????????????????????????????????????????????

            ?????????????????????????????????????????

            >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
            >>> non_null = string1 or string2 or string3
            >>> non_null
            'Trondheim'

            ?????????Python??C??????????????????????C ?????????????????????????????????C????????????????????????????==????????????????

             
            5.7
            ???????????????

            ?????????????????????????????? ?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????ASCII??????????????????????????????

            (1, 2, 3)              < (1, 2, 4)
            [1, 2, 3]              < [1, 2, 4]
            'ABC' < 'C' < 'Pascal' < 'Python'
            (1, 2, 3, 4)           < (1, 2, 4)
            (1, 2)                 < (1, 2, -1)
            (1, 2, 3)             == (1.0, 2.0, 3.0)
            (1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)

            ????????????????????????????????????????????????????????????????????????????????list??????????????????string??????????????string?????????????tuple??????????????????????????????????????0????0.0??????5.1



            ???

            ... etc.5.1
            ???????????????????????????????????Python??????????

            Previous Page

            Up One Level

            Next Page

            Python ???

            Contents

            ????? 4. ??????????? ????? Python ??? ??? 6. ???


            Release 2.3, documentation updated on July 29, 2003.

            See About this document... for information on suggesting changes.
            ?????, @ssv
            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      ŷ