Python Data Structures
1. list
2. stack
3. queue
4. tuple
5. sequence
6. set
7. dict
# -*- coding: utf-8 -*-
# 添加中文注释
'''
Created on 2011-4-29
test for python data structure
@author: xuqiang
'''
###################list##################
print("test for list");
a = [66.25, 333, 333, 1, 1234.5];
# 打印元素出现的次数
print(a.count(333), a.count('a'));
a.insert(2, -1);
print(a);
# 返回首次数显该元素的数组下标
a.index(333);
# 反转该list
a.reverse();
# 排序
a.sort();
##################stack#####################
print("use list as a stack");
stack = [1, 2, 3, 4]
print("push 5");
stack.append(5);
print("pop the stack");
stack.pop();
print(stack);
###############queue###################
from collections import deque
queue = deque(["hello", "world", "to"]);
# 入队
queue.append("you");
print(queue);
# 出队列
queue.popleft();
print(queue);
# 测试filter函数
def f(x): return x % 2 != 0 and x % 3 != 0
print(filter(f, range(2, 25)));
# 测试map函数
def cube(x): return x*x*x
print(map(cube, range(1, 11)));
# 函数可以传递多个参数
seq = range(8);
def add(x, y):
return x + y;
print(map(add, seq, seq));
# 测试reduce函数,该函数迭代进行
print(reduce(add, range(1, 11)));
def sun(seq):
return reduce(add, seq, 0);
print(sum(range(5)));
freshfruit = [' banana', ' loganberry ', 'passion fruit ']
#生成list
print([ weapon.strip() for weapon in freshfruit ]);
vec = [2, 4, 6];
print([ 3 * x for x in vec ]);
print( [ x * 3 for x in vec if x > 2 ] );
print( [ [x, x + 2] for x in vec if x > 2 ] );
vec1 = [1, 2, 3]
vec2 = [1, 2, 3]
# 遍历两个数组
print( [ x + y for x in vec1 for y in vec2 ] );
# 嵌套list
mat = [ [1, 2, 3], [4, 5, 6] ];
# 遍历数组
for i in [0, 1, 2] :
for row in mat:
print(row[i]);
# 还是遍历
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip( questions, answers ):
print 'What is your {0}? It is {1}.'.format(q, a)
# 测试 del方法
a = [-1, 1, 66.25, 333, 333, 1234.5];
del a[0];
print(a);
######################tuple##############################
t = 12345, 54321, 'hello';
t[0]; # 得到第一个 元素
print(t);
# 嵌套tuple
u = t, (1, 2, 3, 4, 5);
print(u);
empty = (); # 空tuple
singleton = 'hello'; # 只含有 一个元素
print(len(empty));
print(len(singleton));
####################set############################
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
fruit = set(basket) # create a set without duplicates
print(fruit);
print( 'orange' in fruit );
# 集合交,并
a = set('abracadabra');
b = set('alacazam');
print(a | b);
print(a & b);
############################dict##############################
# 构造函数
dict([(x, x**2) for x in (2, 4, 6)]);
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]);
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127; # 如果不存在将默认增加
tel['jack'] = 0; # 存在的话,默认是修改
print(tel);
# 便利
for key in tel.keys() :
print key;
print tel[key];
# 另外一种遍历形式
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for i, v in knights.iteritems() :
print i, v;
# 添加中文注释
'''
Created on 2011-4-29
test for python data structure
@author: xuqiang
'''
###################list##################
print("test for list");
a = [66.25, 333, 333, 1, 1234.5];
# 打印元素出现的次数
print(a.count(333), a.count('a'));
a.insert(2, -1);
print(a);
# 返回首次数显该元素的数组下标
a.index(333);
# 反转该list
a.reverse();
# 排序
a.sort();
##################stack#####################
print("use list as a stack");
stack = [1, 2, 3, 4]
print("push 5");
stack.append(5);
print("pop the stack");
stack.pop();
print(stack);
###############queue###################
from collections import deque
queue = deque(["hello", "world", "to"]);
# 入队
queue.append("you");
print(queue);
# 出队列
queue.popleft();
print(queue);
# 测试filter函数
def f(x): return x % 2 != 0 and x % 3 != 0
print(filter(f, range(2, 25)));
# 测试map函数
def cube(x): return x*x*x
print(map(cube, range(1, 11)));
# 函数可以传递多个参数
seq = range(8);
def add(x, y):
return x + y;
print(map(add, seq, seq));
# 测试reduce函数,该函数迭代进行
print(reduce(add, range(1, 11)));
def sun(seq):
return reduce(add, seq, 0);
print(sum(range(5)));
freshfruit = [' banana', ' loganberry ', 'passion fruit ']
#生成list
print([ weapon.strip() for weapon in freshfruit ]);
vec = [2, 4, 6];
print([ 3 * x for x in vec ]);
print( [ x * 3 for x in vec if x > 2 ] );
print( [ [x, x + 2] for x in vec if x > 2 ] );
vec1 = [1, 2, 3]
vec2 = [1, 2, 3]
# 遍历两个数组
print( [ x + y for x in vec1 for y in vec2 ] );
# 嵌套list
mat = [ [1, 2, 3], [4, 5, 6] ];
# 遍历数组
for i in [0, 1, 2] :
for row in mat:
print(row[i]);
# 还是遍历
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
for q, a in zip( questions, answers ):
print 'What is your {0}? It is {1}.'.format(q, a)
# 测试 del方法
a = [-1, 1, 66.25, 333, 333, 1234.5];
del a[0];
print(a);
######################tuple##############################
t = 12345, 54321, 'hello';
t[0]; # 得到第一个 元素
print(t);
# 嵌套tuple
u = t, (1, 2, 3, 4, 5);
print(u);
empty = (); # 空tuple
singleton = 'hello'; # 只含有 一个元素
print(len(empty));
print(len(singleton));
####################set############################
basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
fruit = set(basket) # create a set without duplicates
print(fruit);
print( 'orange' in fruit );
# 集合交,并
a = set('abracadabra');
b = set('alacazam');
print(a | b);
print(a & b);
############################dict##############################
# 构造函数
dict([(x, x**2) for x in (2, 4, 6)]);
dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]);
tel = {'jack': 4098, 'sape': 4139}
tel['guido'] = 4127; # 如果不存在将默认增加
tel['jack'] = 0; # 存在的话,默认是修改
print(tel);
# 便利
for key in tel.keys() :
print key;
print tel[key];
# 另外一种遍历形式
knights = {'gallahad': 'the pure', 'robin': 'the brave'}
for i, v in knights.iteritems() :
print i, v;
如果您觉得不错,欢迎扫码支持下。
作者:许强1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。 2. 本文版权归作者和博客园共有,转载时,请保留本文链接。