Python 数据结构学习

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 "", line 1, in 
NameError: name 'queeu' is not defined
>>> queue.popleft()
'Eric'
>>> queue
deque(['John', 'Michael', 'Terry', 'Graham'])

tuple

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

你可能感兴趣的:(Python 数据结构学习)