列表list

List是python的一个内置动态数组对象,它的基本使用方式如下:

shoplist = ['apple', 'mango', 'carrot', 'banana']

print
'I have', len(shoplist),'items to purchase.'

print
'These items are:', # Notice the comma at end of the line
    for item in shoplist:
        print item,

print
'\nI also have to buy rice.'
    shoplist.append('rice')
    print
'My shopping list is now', shoplist

print
'I will sort my list now'
    shoplist.sort()
    print
'Sorted shopping list is', shoplist

print
'The first item I will buy is', shoplist[0]
    olditem = shoplist[0]
    del shoplist[0]

print
'I bought the', olditem
    print
'My shopping list is now', shoplist

元组Tuple

Python的typle通过小括号初始化,是一个只读对象。不过它的成员具有数组的访问方式。

zoo = ('wolf', 'elephant', 'penguin')
    print
'Number of animals in the zoo is', len(zoo)

new_zoo = ('monkey', 'dolphin', zoo)
    print
'Number of animals in the new zoo is', len(new_zoo)
    print
'All animals in new zoo are', new_zoo
    print
'Animals brought from old zoo are', new_zoo[2]
    print
'Last animal brought from old zoo is', new_zoo[2][2]

字典dict

Dict是一个查询式的数据结构,它的基本用法如下:

ab = {    'Swaroop' : 'swaroopch@byteofpython.info',
        'Larry' : 'larry@wall.org',
        'Matsumoto' : 'matz@ruby-lang.org',
        'Spammer' : 'spammer@hotmail.com'
        }

print
"Swaroop's address is %s" % ab['Swaroop']

# Adding a key/value pair
ab['Guido'] = 'guido@python.org'

# Deleting a key/value pair
del ab['Spammer']

print
'\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
    print
'Contact %s at %s' % (name, address)

if
'Guido'
in ab: # OR ab.has_key('Guido')
    print
"\nGuido's address is %s" % ab['Guido']

Python学习笔记——基本数据结构的更多相关文章

  1. python学习笔记五——数据结构

    4 . python的数据结构 数据结构是用来存储数据的逻辑结构,合理使用数据结构才能编写出优秀的代码.python提供的几种内置数据结构——元组.列表.字典和序列.内置数据结构是Python语言的精 ...

  2. Python学习笔记系列——数据结构相关

    Python有4种数据结构:列表(list).字典(dictionary).元组(Tuple).集合(set).从最直接的感官上来说,这四种数据结构的区别是:列表中的元素使用方括号括起来,字典和集合是 ...

  3. Python学习笔记(3)--数据结构之列表list

    Python的数据结构有三种:列表.元组和字典 列表(list) 定义:list是处理一组有序项目的数据结构,是可变的数据结构. 初始化:[], [1, 3, 7], ['a', 'c'], [1, ...

  4. Python学习笔记(5)--数据结构之字典dict

    字典(dict) 定义:键值对集合 初始化:{}, {'1' : 'abc', '2' : 'def'} 1.增加:单个数据直接赋值  update(dict2) ---把dict2的元素加入到dic ...

  5. Python学习笔记(4)--数据结构之元组tuple

    元组(tuple) 定义:tuple和list十分相似,但是tuple是不可变的,即不能修改tuple 初始化:(), ('a', ) , ('a', 'b')   //当只有一个元素时,需加上逗号, ...

  6. python学习笔记整理——字典

    python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...

  7. python学习笔记整理——元组tuple

    Python 文档学习笔记2 数据结构--元组和序列 元组 元组在输出时总是有括号的 元组输入时可能没有括号 元组是不可变的 通过分拆(参阅本节后面的内容)或索引访问(如果是namedtuples,甚 ...

  8. python学习笔记之module && package

    个人总结: import module,module就是文件名,导入那个python文件 import package,package就是一个文件夹,导入的文件夹下有一个__init__.py的文件, ...

  9. Python学习笔记,day5

    Python学习笔记,day5 一.time & datetime模块 import本质为将要导入的模块,先解释一遍 #_*_coding:utf-8_*_ __author__ = 'Ale ...

随机推荐

  1. Spring 笔记(四)AOP

    前言 横切关注点 使用 @AspectJ 定义切面. 同时还需要在配置类上应用 @EnableAspectJAutoProxy 注解,启用 AOP 自动代理.(不添加它的话,@AspectJ 注解的类 ...

  2. 01、dos命令行的常用命令

    cd 进入指定目录cd..  返回上一级目录cd\   退回盘符根目录dir        列出当前目录下的文件以及文件夹md       创建目录rd 删除目录del   删除文件cls       ...

  3. 【bzoj2959】长跑 LCT+并查集

    题目描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘,摩肩接踵,盛况空前.为了 ...

  4. BZOJ 1731: [Usaco2005 dec]Layout 排队布局

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  5. P2324 [SCOI2005]骑士精神

    题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位.两组数据之间没有空行. 输出格式 ...

  6. Tomcat学习笔记(六)

    Tomcat日志 网上找到一篇分析不错的博文   http://peiquan.blog.51cto.com/7518552/1580189/

  7. 类复制 MemberwiseClone与Clone(深 浅 Clone)

    MemberwiseClone 方法创建一个浅表副本,具体来说就是创建一个新对象,然后将当前对象的非静态字段复制到该新对象.如果字段是值类型的,则对该字段执行逐位复制.如果字段是引用类型,则复制引用但 ...

  8. bzoj 4897 天赋 有向图的矩阵数定理

    4894: 天赋 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 104  Solved: 80[Submit][Status][Discuss] De ...

  9. java 复习整理(三 修饰符)

    访问控制修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java支持4种不同的访问权限. 默认的,也称为default,在同一包内可见,不使用任何修饰符. 私有的,以pr ...

  10. 通过RHN网站给RHEL打补丁

    [root@yum01 ~]# yum list-sec securityLoaded plugins: downloadonly, product-id, rhnplugin, security, ...