## 列表的基本操作：

### 定义：

``````# 定义成由数字组成：
l_abc = [3, 2, 3, 4, 6]
``````

### 列表的遍历（iterator）

``````for abc in l_abc:
print abc,

# 3 2 3 4 6 9
``````

``````# 列表切片 l_abc[1:3]，列表的第 2 到 3 位

for i, abc in enumerate(l_abc[1:3]):
print i, abc
``````

### 列表的增加

``````l_abc.append(33)
for abc in l_abc:
print abc,

#3 2 3 4 6 9 33
``````

### 列表的删除

``````# 从后面删
val = l_abc.pop()  # equal to l_abc.pop(-1)
for abc in l_abc:
print abc,

#3 2 3 4 6 9

# 从前面删
val = l_abc.pop(0)
for abc in l_abc:
print abc,
#2 3 4 6 9
``````

### 高级用法与 numpy 的使用：

#### 方法1 生成 0~1 的 0.05 step 等差数组：

``````#方法1 先生成 0~100：
l_abc = range(0, 100, 5)
``````

``````l_abc2 = []
for abc in l_abc:
l_abc2.append(abc/float(100))

``````

``````l_abc2 = [ abc/float(100) for abc in l_abc ]
``````

map 用法详见：map

``````def div_100(val, divVal=100):
return val/float(divVal)

l_abc2 = map(div_100, l_abc)
``````

lambda 函数用法详见：lambda

``````l_abc2 = map(lambda x: x/float(100), l_abc)
``````

``````# 数字字符串转换成列表字符串
l_abc2_strOut = map(lambda x: "%1.2f" % x , l_abc2)

#打印
print "t".join(l_abc2_strOut)
``````

#### 方法2 使用 numpy.

numpy 是 python 科学计算的专用模块。建议以后的数据运算使用这种。

``````import numpy as np
np_abc = np.linspace(0, 1, 21)
``````

``````l_abc2_strOut = map(lambda x: "%1.2f" % x , np_abc)
print "t".join(l_abc2_strOut)
``````

## 字典的基本操作

### 基本用法

``````M_abc = {
'a' : 0,
'b' : 1,
'c' : 2,
'd' : 3,
}
``````

``````l_1 = ['a', 'b', 'c', 'd']  # key
l_2 = range(4)              # value
M_abc = dict(zip(l_1, l_2))
``````

``````print M_abc['a']
# 0
``````

``````del M_abc['a']
M_abc['a'] = 0
``````

``````for key in M_abc:
print key, M_abc[key]
``````

``````print M_abc.keys()
``````

``````
l_abc = ['c', 'a', 'e', 'b']
for abc in l_abc:
if abc in M_abc:
print M_abc[abc]

"""output
2
0
1
"""
``````

### 复杂用法

``````import functools
l_out = map(functools.partial(lambda y, x: y[x] if x in y else None, M_abc), l_abc)

# 以上等价于
def hasVal(M_abc, abc):
out = None
if abc in M_abc:
out = M_abc[abc]
return out

l_out = map(functools.partial(hasVal, M_abc), l_abc)

#扔掉空值
l_out = filter(lambda x: 0 if x is None else 1, l_out)
print "n".join(l_out)

"""output
2
0
1
"""
``````