元组
- 创建元组
tup1 = 1, 2, 3
print tup1
# 嵌套元组
tup2 = (1, 2, 3), (4, 5)
print tup2
(1, 2, 3)
((1, 2, 3), (4, 5))
- 转换为元组,list->tuple, string->tuple
l = [1, 2, 3]
print tuple(l)
str = 'Hello ChinaHadoop'
print tuple(str)
(1, 2, 3)
('H', 'e', 'l', 'l', 'o', ' ', 'C', 'h', 'i', 'n', 'a', 'H', 'a', 'd', 'o', 'o', 'p')
- 访问元组
tup3 = tuple(str)
print tup3[4]
o
- 合并元组
tup1 + tup2
(1, 2, 3, (1, 2, 3), (4, 5))
- 拆包
a, b, c = tup1
print b
2
# 函数返回多个值
def return_multiple():
t = (1, 2, 3)
return t
a, _, c = return_multiple()
print c
3
# 元组列表迭代
tuple_lst = [(1, 2), (3, 4), (5, 6)]
for x, y in tuple_lst:
print x+y
3
7
11
- count 方法
tup3.count('o')
3
列表
- 创建列表
lst_1 = [1, 2, 3, 'a', 'b', (4, 5)]
print lst_1
lst_2 = range(1, 9)
print lst_2
[1, 2, 3, 'a', 'b', (4, 5)]
[1, 2, 3, 4, 5, 6, 7, 8]
- 转换为列表, tuple->list
lst_3 = list(tup3)
print lst_3
['H', 'e', 'l', 'l', 'o', ' ', 'C', 'h', 'i', 'n', 'a', 'H', 'a', 'd', 'o', 'o', 'p']
- 添加、移除元素
lst_4 = range(10)
# 末尾添加
lst_4.append(11)
print lst_4
# 指定位置插入
lst_4.insert(5, 12)
print lst_4
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
[0, 1, 2, 3, 4, 12, 5, 6, 7, 8, 9, 11]
# 删除指定位置的元素并返回
item = lst_4.pop(6)
print item
print lst_4
5
[0, 1, 2, 3, 4, 12, 6, 7, 8, 9, 11]
# 删除指定的值,注意12在这里是“值”不是“位置”
lst_4.remove(12)
print lst_4
[0, 1, 2, 3, 4, 6, 7, 8, 9, 11]
- 合并列表
lst_3 = lst_1 + lst_2
print lst_3
lst_1.extend(lst_2)
print lst_1
[1, 2, 3, 'a', 'b', (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 'a', 'b', (4, 5), 1, 2, 3, 4, 5, 6, 7, 8]
- 排序操作
import random
lst_5 = range(10)
random.shuffle(lst_5)
print lst_5
[7, 8, 5, 3, 2, 0, 4, 6, 9, 1]
lst_5.sort()
print lst_5
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
lst_6 = ['Welcome', 'to', 'Python', 'Data', 'Analysis', 'Course']
lst_6.sort()
print lst_6
['Analysis', 'Course', 'Data', 'Python', 'Welcome', 'to']
lst_6.sort(key = len, reverse=True)
print lst_6
['Analysis', 'Welcome', 'Course', 'Python', 'Data', 'to']
print lst_5
print lst_5[1:5]
print lst_5[5:]
print lst_5[:5]
print lst_5[-5:]
print lst_5[-5:-2]
print lst_5[::2]
print lst_5[::-1]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4]
[5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[5, 6, 7]
[0, 2, 4, 6, 8]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
字典 dict
- 创建字典
empty_dict = {}
dict1 = {'a': 1, 2: 'b', '3': [1, 2, 3]}
print empty_dict
print dict1
{}
{'a': 1, 2: 'b', '3': [1, 2, 3]}
- 插入元素
dict1[4] = (4, 5)
print dict1
{'a': 1, 2: 'b', 4: (4, 5), '3': [1, 2, 3]}
- 删除元素
del dict1[2]
print dict1
{'a': 1, 4: (4, 5), '3': [1, 2, 3]}
a_value = dict1.pop('a')
print a_value
print dict1
1
{4: (4, 5), '3': [1, 2, 3]}
- 获取键、值列表
print dict1.keys()
print dict1.values()
[4, '3']
[(4, 5), [1, 2, 3]]
- 合并字典
dict2 = {4: 'new1', 5: 'news'}
dict1.update(dict2)
print dict1
{'3': [1, 2, 3], 4: 'new1', 5: 'news'}
- 通过多个列表创建字典
# 普通方法
dict_3 = {}
l1 = range(10)
l2 = list(reversed(range(10)))
for i1, i2 in zip(l1, l2):
dict_3[i1] = i2
print dict_3
{0: 9, 1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 3, 7: 2, 8: 1, 9: 0}
dict_4 = dict(zip(l1, l2))
print dict_4
{0: 9, 1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 3, 7: 2, 8: 1, 9: 0}
hash函数
hash(12)
12
hash('test hash')
-520327859
hash((1, 2, 3))
-378539185
hash([1, 2, 3])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-0b995650570c> in <module>()
----> 1 hash([1, 2, 3])
TypeError: unhashable type: 'list'
集合 set
a = set(range(10))
print a
b = set(range(5,15))
print b
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
set([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
- 并 交 差 异或
a | b
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
a & b
{5, 6, 7, 8, 9}
a - b
{0, 1, 2, 3, 4}
a ^ b
{0, 1, 2, 3, 4, 10, 11, 12, 13, 14}
- 判断是否为子集、父集
a.issubset(b)
False
a.issuperset(b)
False
Python高阶函数
- 函数式编程
函数本身也可赋值给变量
import math
math.sqrt(25)
输出:
5.0
math.sqrt
输出:
fun = math.sqrt
fun
fun(10)
输出:
3.1622776601683795
将函数作为参数
def func_add(x, y, f):
"""
functional addition
"""
return f(x) + f(y)
print func_add(4, 25, math.sqrt)
print func_add(-4, 25, abs)
7.0
29
map/reduce
- map
x_2_lst = [x**2 for x in range(10)]
print x_2
x_sqrt_lst = map(math.sqrt, x_2)
print x_sqrt_lst
x_2_float_lst = map(float, x_2)
print x_2_float_lst
x_2_str_lst = map(str, x_2)
print x_2_str_lst
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0]
['0', '1', '4', '9', '16', '25', '36', '49', '64', '81']
- reduce
str_lst = map(str, range(5)) # ['0', '1', ...]
print str_lst
def make_num(str1, str2):
return int(str1) * 10 + int(str2)
result = reduce(make_num, str_lst)
print result
['0', '1', '2', '3', '4']
1234
规范字符串
name_lst = ['poNNY MA', 'rObIN li', 'steve JOBS', 'bILL gates']
standard_name_lst = map(str.title, name_lst)
print standard_name_lst
['Ponny Ma', 'Robin Li', 'Steve Jobs', 'Bill Gates']
filter
number_lst = range(-10, 10)
def is_negative(x):
return x < 0
filtered_lst = filter(is_negative, number_lst)
print number_lst
print filtered_lst
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]
map reduce filter 与匿名函数
匿名函数:lambda
- 没有函数名
- 单条语句组成
- 语句执行的结果就是返回值
- 可用作sort的key的函数
- map与匿名函数
x_lst = range(10)
result_lst = map(lambda item : item**2 +item**3, x_lst)
#相当于事先定义了一个平方加立方的函数,但是用匿名函数就看起来特别简洁
print x_lst
print result_lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]
- reduce与匿名函数
x_lst = range(1, 5)
product = reduce(lambda x, y : x*y, x_lst)
#记住这里 reduce(func(x,y), lst) ,其中func必须有两个参数。每次计算结果和下一个元素做累积计算
print x_lst
print product
[1, 2, 3, 4]
24
- filter与匿名函数
number_lst = range(-10, 10)
filtered_lst = filter(lambda x : x<0, number_lst)
#filter(func, lst ), 将func作用于lst的每个元素,然后根据返回值是 TRUE还是FALSE 判断是保留还是丢弃该元素。
print number_lst
print filtered_lst
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1]