列表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. 什么是EM算法?

    开头借用李航老师书中总结,概率模型有时既含有观测变量,又含有隐藏变量或者潜在变量,如果概率模型的变量都是观测变量,那么给定数据,可以直接用极大似然估计法,或者贝叶斯估计法估计模型参数,但是,当模型含有 ...

  2. Java 复习计划

    前言 打算下学期开学,也就是九月份,去找实习,现在还有三个月时间.(然而还在天天玩 Python..) 定个复习计划. 1. 基础 并发:Java并发编程实战 [ x ] SQL:MySQL,看看书, ...

  3. 团队项目-第八次scrum 会议

    时间:11.4 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 修复员工招聘时bug 完成员工commit函数的数值函数编写 陈鑫 实现雇佣与解雇功能的界 ...

  4. nyoj 题目49 开心的小明

    开心的小明 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天 ...

  5. LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。

    LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型

  6. 【R】多元线性回归

    R中的线性回归函数比较简单,就是lm(),比较复杂的是对线性模型的诊断和调整.这里结合Statistical Learning和杜克大学的Data Analysis and Statistical I ...

  7. easyui中的依赖关系

    参考自:http://www.easyui.info/archives/765.html 在使用easyui的过程中发现各个组件直接存在依赖关系,也就是上层的复杂组件依赖于一个或者多个简单组件,复杂组 ...

  8. MPLAB® XC C编译器的Workstation License的获取及安装方法

    MPLAB®XC C编译器的Workstation License获取及安装方法如下:首先需要购买获得一个XC C编译器的激活码,然后到以下网页(http://www.microchip.com/rl ...

  9. synflood 模拟工具

    synflood 模拟工具 来源 https://blog.csdn.net/wuzhimang/article/details/54581117 因项目需要,要对主流的几家抗DDoS设备做测评,当然 ...

  10. [洛谷P4178]Tree

    题目大意:给一棵树,问有多少条路径长度小于等于$k$ 题解:点分治 卡点:无 C++ Code: #include <cstdio> #include <algorithm> ...