第2章:量化语言–Python
2.1数据结构
*2.1.1基本类型
i = 1
print type(i)
if isinstance(i, str):
print “i is str type”
2.1.2字符串和容器
字符串是不可变对象,对字符串操作会返回新的对象
str = ‘I love you’
print id(str)
str_new = str.replace(’ ', ‘’)
print id(str_new)
2.2函数
zip()函数:同时迭代
dict = {‘key1’:‘value1’, ‘key2’: ‘value2’}
min(zip(dict.values(), dict.keys()))
输出:(‘value1’, ‘key1’)
lambda函数:
y = lambda x: x*x
print y(5)
输出:25
map()函数: 将序列元素代入函数
x_list = [1,2,3,4,5]
y = map( lambda x: x*x , x_list)
print y
输出:[1, 4, 9, 16, 25]
filter()函数:把序列元素代入函数。返回值是True则保留序列元素
def is_odd(n):
return n % 2 == 1
x = [1, 2, 3, 4, 5]
y = filter(is_odd, x)
print y
输出:[1, 3, 5]
reduce()函数:把序列元素代入函数。函数得有两个参数,先对序列中第1,2个参数操作,得到的结果再与第3个操作,依次
x = [1, 2, 3, 4, 5]
y = reduce(lambda a, b: a*b,