python的基本数据结构_Python学习笔记——基本数据结构

列表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学习笔记五——数据结构

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

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

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

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

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

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

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

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

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

python学习笔记整理——字典

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

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

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

python学习笔记之module && package

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

Python学习笔记,day5

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

随机推荐

Java中Properties类知识的总结

一.Properties类与配置文件 注意:是一个Map集合,该集合中的键值对都是字符串.该集合通常用于对键值对形式的配置文件进行操作. 配置文件:将软件中可变的部分数据可以定义到一个文件中,方便以后 ...

Java完成最简单的WebService创建及使用(REST方式,Jersey框架)

前言: 一直以来都对WebService感兴趣,但因为难以理解WebService到底是什么,所以了解甚少.周二的时候有个跟我关系比较好的同事想要自己写个WebService的小Demo,希望能够做成 ...

有关mipmaps

Mipmaps的作用是什么,仅仅是为了使屏幕三角形和纹理三角形的差异变小?有没有以空间换时间的理念? Mipmaps在生成一系列小的纹理样本时, 是如何从原始纹理采样的?即如何生成这些小的纹理样本.

sed命令实战

删除所有的空行,并在每行后面增加一个空行 sed '/^$/d;G' /etc/fstab 将每一行前导的“空白字符”(空格,制表符)删除 sed 's/^[\t ]*//' file 将文本中的 a ...

Python爬虫——抓取贴吧帖子

抓取百度贴吧帖子 按照这个学习教程,一步一步写出来,中间遇到很多的问题,一一列举 首先, 获得 标题 和 贴子总数 # -*- coding:utf-8 -*- #!/user/bin/python ...

全选demo

我们处理数据时,最好能够支持全选操作. 选中之后,进行删除,或其他处理. 我自己写了一个demo. 主要功能: 1.点击全部选中 2.点击全部取消 3.然后进行获取选中的id,进行处理 代码如下: & ...

w3chtml页面和css书写规范

http://www.cnblogs.com/Wenwang/archive/2011/09/07/2169881.html

Centos修改默认网卡名

安装系统后默认的网卡名称为 enpXX ,修改为熟悉的eth0 1 vi /etc/default/grub GRUB_TIMEOUT=5GRUB_DEFAULT=savedGRUB_DISABLE_ ...

vlc 网页插件的 使用与控制 API http://www.xuebuyuan.com/2224602.html

不知道vlc 是什么的请百度一下.. vlc 提供了ie浏览器的activeX插件和火狐或者chrome的插件,基本上覆盖了所有浏览器,所以有复杂解码需求的情况下用vlc来解决网页播放视频,也是一种没 ...

洛谷 4525 && 洛谷 4526 【模板】自适应辛普森法

题目:https://www.luogu.org/problemnew/show/P4525 https://www.luogu.org/problemnew/show/P4526 参考:https: ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值