美文网首页
Python 数据结构学习

Python 数据结构学习

作者: HenryTien | 来源:发表于2017-03-14 12:04 被阅读27次

    5. Data Structures

    https://docs.python.org/3.3/tutorial/datastructures.html

    list 学习

    >>> classmates=['Michael','Bob','Tracy']
    >>> classmates
    ['Michael', 'Bob', 'Tracy']
    >>> classmates[1]='Sarah'
    >>> classmates
    ['Michael', 'Sarah', 'Tracy']
    >>> L=['Apple',123,True]
    >>> L
    ['Apple', 123, True]
    >>> s=['python','java',['asp',123],'google']
    >>> len(s)
    4
    >>> 
    
    >>> a = [66.25, 333, 333, 1, 1234.5]
    >>> print(a.count(333),a.count(66.25),a.count('x'))
    2 1 0
    >>> a.insert(2,-1)
    >>> a.append(335)
    >>> a
    [66.25, 333, -1, 333, 1, 1234.5, 335]
    >>> a.remove(333)
    >>> a
    [66.25, -1, 333, 1, 1234.5, 335]
    >>> a.sort()
    >>> a
    [-1, 1, 66.25, 333, 335, 1234.5]
    >>>
    

    5.1.1. Using Lists as Stacks

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

    5.1.2. Using Lists as Queues

    >>> from collections import deque
    >>> queue=deque(["Eric","John","Michael"])
    >>> queue.append("Terry")
    >>> queue.append("Graham")
    >>> queeu.popleft()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    NameError: name 'queeu' is not defined
    >>> queue.popleft()
    'Eric'
    >>> queue
    deque(['John', 'Michael', 'Terry', 'Graham'])
    

    tuple

    所以,只有1个元素的tuple定义时必须加一个逗号,,来消除歧义:

    相关文章

      网友评论

          本文标题:Python 数据结构学习

          本文链接:https://www.haomeiwen.com/subject/wwntnttx.html