python 列表 list的基本操作

一,Python : list.append(x)

一个加到a[len(a):] = [x]

list.extend(L)
一个加到一个a[len(a):] = L

list.insert(i, x) 一个一个a.insert(0, x) a.insert(len(a), x) a.append(x)

list.remove(x)
x 一个一个

list.pop([i ])
a.pop() 一个(i 你会Python )

list.index(x)
一个x 一个

list.count(x)
x

list.sort()

list.reverse()

二,链使便为一个使一个(先出)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 使

使(先出)很快; (为了一个)

使collections.deque 设计:

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])

>>> queue.append("Terry") >>> queue.append("Graham") >>> queue.popleft()
’Eric’

# Terry arrives
# Graham arrives
# The first to arrive now leaves

>>> queue.popleft()
’John’

 

四,列

为从了一个一些 过返

, 一个 squares , :

目的: = **for in ()]

squares = map(lambda x: x**2, range(10)),

一个一个 for for if 一个for if 上下果构

两个:

:

>>> combs = []
>>> for x in [1,2,3]:

for y in [3,1,4]: if x != y:

...
...
...
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

 

猜你喜欢

转载自www.cnblogs.com/wangyue0925/p/9628166.html