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;

转载于:https://www.cnblogs.com/xuqiang/archive/2011/04/29/2033135.html

Python Data Structures相关推荐

  1. 【Python学习笔记】Coursera课程《Python Data Structures》 密歇根大学 Charles Severance——Week6 Tuple课堂笔记...

    Coursera课程<Python Data Structures> 密歇根大学 Charles Severance Week6 Tuple 10 Tuples 10.1 Tuples A ...

  2. 【Python for Everybody(Python Data Structures)】Week 4 | Chapter 8 题目汇总

    PY4E课程官网:https://www.py4e.com/ 参考文章(Github): ed-lau/python-for-everybody kalpesh14m/Python-For-Every ...

  3. python 科学计算设计_Python科学计算——Data Structures

    为什么选择Python作为科学计算语言? 有关于Matlab和Python哪个更适合作为科学计算语言的争论已久,之所以选择Python作为首选的科学计算语言,不仅仅是因为它免费,开源,有很多优秀的库和 ...

  4. python数据结构那本书好_推荐一本书《Data Structures and Algorithms in Python》

    [ 在 wuhaochi (oo) 的大作中提到: ] : 标  题: Re: 推荐一本书<Data Structures and Algorithms in Python> : 发信站: ...

  5. pandas笔记(pandas Data Structures)

    pandas笔记(pandas Data Structures) 生信start_site已关注 32020.06.15 03:02:37字数 766阅读 509 pandas包含数据结构和数据操作工 ...

  6. Python_Example_ Data Structures and Algorithm Analysis 学习/示例

    Author: 楚格 2018-11-19   19:05:11 IDE: Pycharm2018.02   Python 3.7 KeyWord :  Data Structures and Alg ...

  7. python loading_MXNet Python Data Loading API

    MXNet Python Data Loading API Introduction 介绍 MXNet 数据加载模块的主要特性. Create A Data Iterator 介绍如何在创建一个 py ...

  8. Data Structures with C++ Using STL Chapter 3算法概述---笔记

    <Data Structures with C++ Using STL Chapter 3算法概述---笔记>,作者:茉莉花茶,原文链接:http://www.cnblogs.com/yc ...

  9. Python Data Science的多版本多环境管理工具Anaconda

    简介 python开发中存在2个版本: python2和python3,这2个版本有细微的不同,在OS上安装2个版本并很好地管理各自的依赖包,并不是一个很好的事情. 再者,在团队开发中,每个开发者的环 ...

最新文章

  1. 解决客户端从服务器请求数据乱码问题
  2. java有几种变量_java有多少种变量?java类变量怎么使用?
  3. 用VC++建立Service服务应用程序
  4. [python][pandas]pandas数据处理+直方图绘制
  5. java 前后端分离思想与实现
  6. win8关机快捷键_win8系统电脑使用技巧的详细介绍--win7w.com
  7. Android中的短信收不到问题,华为的安卓(Android)系统手机收不到短信问题解决方法...
  8. 尚硅谷-TypeScript
  9. 利用matlab画地图
  10. 小规模纳税人可以申请美元账户收款么?
  11. 药品名自动归类机器人
  12. NVL函数,NVL2函数的使用,查询日期天数
  13. Node.js怎么配置 ?
  14. Highly Efficient Salient Object Detection with 100K Parameters论文解读
  15. 别老盯着垃圾分类,“垃圾”创业还有许多的突破口
  16. 考研最辛苦、最努力的一批人,喊累之前先看看他们……
  17. 关于SLAM的系列很有价值的网文_拔剑-浆糊的传说_新浪博客
  18. 智慧校园应用系统建设方案
  19. 双线性插值实现图像缩放详解
  20. 3阶以下贝塞尔曲线轨迹库和任意轨迹库

热门文章

  1. 初级java开发学习路线_成为初级全栈Web开发人员的10分钟路线图
  2. 倦怠和枯燥_启动倦怠
  3. 手动部署OpenStack环境(三:OpenStack环境预配置)
  4. 1043 输出PATest
  5. 关于jsp和eclipse服务器端的相关配置和JS的区别
  6. 面试题:2018最全Redis面试题整理
  7. 公有云环境下应用程序的自动化部署与水平扩展问题
  8. HttpApplication事件ASP.NET页面周期
  9. Java 集合 — HashMap
  10. Oracle VS DB2 数据类型